Being Saved by the Bell is getting a last minute reprieve. If you think about it in a classroom experience, a professor might ask you something that you don't know. But before, you have a chance to say anything, time is up for the class. This is particularly appropriate if the end of class is signaled by a bell. You were quite literally saved by the bell.
The idiom comes from the boxing ring. A boxer might be about to lose the fight, but then the bell rings signaling the end of the round. He then has an opportunity to get some strength back before continuing the fight. He was saved by the bell.