In our game, there have come times when something comes out of the blue (we like to keep things really random in our game which can lead to some nasty, unexpected surprises) and it looks like a TPK might be inevitable.
Now, I play this game for what it is. It's a game, people play it to have fun. Because of that, sometimes it's just not in the best interest of the game to allow that random TPK to happen. Now, some people out there might cry out about DM fiat and other such things but whatever. Play your game your way, we'll play our way.
What we've begun to do is allow the DM to play a DEM (Deux Ex Machina) card in certain situations to allow the game to go on. It's not just a DM decision though, In a situation where it's fairly obvious that a random roll has brought about something that just will not end well, the DM can tell the players that this is an opportunity for a DEM.
The Players are the ones who actually decide on whether to play the DEM card or not though. It is the Players decision because to play a DEM Card will cost the players a certain number of XP, like 5,000 XP or more, at the end of the game, for each DEM Card played.
This way, the game can go on if the players decide it would be more fun to continue onward or to just let things happen as they will, risking almost certain TPK. Only the DM can offer to play the DEM card though, the players cannot ask for it. The players can only vote to take it or not when it is offered and whatever they choose, the DM will go with.
Doing it that way allows the DM to keep things from getting out of hand because of poor playing on the players parts.
So far, we like it and in the last few weeks, it really hasn't been used very often at all despite them knowing it can be used. It has actually been turned down a couple of times.