In medieval times, a tally stick was used to record and document attendance at school and at church, quantities of items, payments made and payments due in commerce, the collection of taxes by local sheriffs, and more. In fact, in England, the split tally of the Exchequer was in continuous use from Medieval times through to 1826.
The tally stick was a stick of wood upon which a ‘nick’ was made to accurately record information. In many cases, time was pressing and so it was important to have the information recorded before the individual with the tally stick was no longer available to ‘nick’ the stick in favour of the individual. If you arrived just before the recording ended, you had done so ‘just in the nick of time.’
In time, the phrase came to mean that if you do something in the nick of time, you do it at the very last minute or second.