It originates from boxing, where the bell signals the end of a round. If a boxer is in trouble and the bell rings just in time, he is "saved by the bell" from further punishment.
Here are some examples of how the idiom can be used:
* "I was about to give up on my project, but then a brilliant idea struck me. I guess I was saved by the bell!"
* "The team was losing badly, but a last-minute goal in the final seconds saved them by the bell."
* "The deadline was approaching fast, and I was panicking. But I managed to finish the work just in time – saved by the bell again!"
The idiom is often used humorously to emphasize the close call and the relief felt at being spared.