"
Buliwyf: Luck often enough, will save a man, if his courage hold." (13th Warrior)
http://www.imdb.com/title/tt0120657/quotes
I appreciate the comments by several others here, and have had similar experiences, motivations and techniques as a DM--but I learned some things too, so thanks for sharing!
I have never dealt a TPK though I came close once, years ago. A gorgon petrified all but one member of the party--including the paladin with his charisma-enhanced saves. The druid managed to save and escaped from the treacherous cove in animal form. Later he returned with the magical means to quietly restore the other party members, and they subsequently--and more cautiously/strategically--overcame their monstrous, deadly foe.
It has been many years since I was even tempted to invoke deus ex machina to avoid TPK by sparing the last hapless character. Nor do I intentionally shift the tide of battle--you all know it: the point in RPG or CRPGs/Strategy Games were there is a "sense" or "feeling" more than raw calculation that you will be victorious (later proved out, and therefore such assessments become more accurate and refined with experience).
There is much creative/story tension in my campaign situations--including combats. I meta-joked recently that I wasn't out to get them but the veterans were reservedly sarcastic. I suppose I give no quarter. But we all have learned over many years and campaigns to ride that edge of dangerous challenge. Numerous times they have overcome foes and odds that seemed hopeless at the outset--even ones not intended for actual resolution (CR 4+ levels higher). They do some requisite "helping the other guy with healing as he reaches -10 + CON score and certain death" but tend to be individualists otherwise.
For my part, I try to have solo or group foes behave according to their INT, instincts, cunning, or other motivations (that is, some rush in, some mob the first or the magic guy, some harry and retreat, some parlay, etc.). Monsters tend to be advanced a little more than standard but not so much that the CR bumps up according to the rules, heh. The players get high 32-point buy starting out for deadlier campaigns, rerolls on 1 for HP after 1st level, etc.
And in closing, to emphasize the "courage" point: there is one younger, long-time player that is determined without being reckless. He has learned by my DM style that I tried to simulate (logical if not "real") situations, and he has proved again and again that he can help turn the tide of battle (whether rogue, cleric, etc.) by being cool under pressure and threat of certain death. His example usually rallies the others who worry what I have unleashed upon them yet again and become hesitant. [I am reminded of the concept of samurai warriors accepting death and the resulting reactiveness and focus empowering them with a capability to overcome most foes--cutting without being cut]
I don't provide openings to succeed: rather, instead, they make those opportunities against determined, deadly foes. Of course, who is the deadlier in such cases? =)