It means to be spared from a difficult situation at the last possible moment. In England in the late 17th century, a sentry at Windsor Castle was accused of being asleep on duty. His defense at his court-martial was that since he had heard the clock of St. Paul's in London, 20 miles away, strike 13 at midnight, he could not have been asleep.
The court ridiculed the idea that the bells of St. Paul's could carry between London and Windsor, and sentenced him to death. It was later verified, however, that the clock did strike 13 instead of 12 times on that particular night. Saved by the bell, the sentry was released and lived to the ripe old age of 102.