These days, “in the nick of time” means just in time or at the last possible moment.
However, the expression began prior to the 1500s as “pudding time.” In those medieval times, pudding was a savory dish, usually made of sausage or haggis, and was served at the beginning of a meal. Therefore, to arrive at pudding time was to arrive just in time to eat. This phrase is first seen in print in John Heywood’s glossary A dialogue conteinyng the nomber in effect of all the prouerbes in the Englishe tongue, 1546.
So why did the Tudors change it to “the nick of time”? One can only assume that people wanted to express a more accurate time than “around the beginning of a meal.” At that time, the “nick” referred to was a notch or a small cut on a tally stick used to keep score or to measure, and was synonymous with “precision.” Or, perhaps, it was simply that pudding had become the name for sweet dishes, normally served at the end of the meal, and thus “pudding time” was no longer accurate for “just in time.”
The “time” in “the nick of time” isn’t necessary, as nick itself refers to time. The first example of the phrase as it’s used today appears in Arthur Day’s Festivals, 1615: “Even in this nicke of time, this very, very instant.”
In Shakespeare’s time, if someone were “in the (very) nick” they were in the precise place at the precise time. Ben Jonson makes a reference to watches and musical instruments being adjusted to precise pre-marked nicks in the play Pans Anniversary, circa 1637: “For to these, there is annexed a clock-keeper, a grave person, as Time himself, who is to see that they all keep time to a nick.”
The word “nick” is not limited to time, but is used to mean many different things. For example, for the English, “in the nick” means “in prison,” for the Scots, “in the valley between two hills,” and for Australians, “naked.” Another British meaning is “theft,” as in “my car’s been nicked.” It’s also another name for the devil, as in “Old Nick.”
The meanings do vary wildly, but we could put them together like this: “I’m in the nick because I was caught running naked between two hills, looking for my nicked car.”