You can use this expression when:
- There's a bad situation.
- It seems like the situation is going to get better, BUT...
- Something else bad happens.
You can always use it in the opposite situation, when it seems like something bad is going to happen, but then you get saved:
Just as I started to think that we were lost, Samantha found the trail again.