I don't do TPKs as a DM unless they are plot-relevant. I have had two that I can recall:
1. In a fairly lengthy campaign, I had the PCs running two seperate groups; their regular PCs, and a gang of orcs, goblins, and bugbears who were working for the local head baddie. The second group was an intermission tale; once every couple of hours, as story demanded, we would switch to that group for a time, and the PCs would get to run errands for the villain, see a side of the tale invisible to their heroes, and have fun being evil. The storyline eventually wound up to the finale: the villain they worked for being annihilated by a much more ominous villain from days of yore, returning to take his rightful place as chief nemesis of the campaign. The PCs were given an option to escape, swear fealty, or defend their boss.....they chose the final one, an act of loyalty, and it was a slaughter. Everyone had a great time, though it helped that they could play their monster PCs fatalistically.
2. The second TPK was for a party of about 5 who were trumped by the villain of the campaign, they walked in to a trap which was almost guaranteed to kill one of them, and the rest fought valiantly but died. The storyline involved the villain capturing their souls, reanimating their bodies for himself and his arch lieutenants to possess as they returned to the kingdom to start a new era of evil rule; they couldn't let the heroes' souls pass on in to the afterlife (lest they warn their gods), so they were imprisoned in soul gems and sold to a liche, who was a prior nemesis from an earlier adventure who wanted very badly to mess the PCs up. The next game session, everyone was ready with new PCs.....but discovered that the story continued, a year later, when they had a chance to escape the soul gems and occupy homunculi bodies which they could shape in to their own chosen forms in the liche's laboratory.....resurrecting them, sans equipment!
The story was a pleasant surprise for all; they had been convinced they were goners, and instead discovered that they could return, and then seek out revenge against the villain who now had a year to commit terrible mischief.....in their old bodies! It was a wonderful twist.
As for unplanned TPKs: I don't believe in it, but it's not out of the question, and it has happened in my younger days of gaming. A a rule, I feel TPKs are disruptive to the storyline and character development, and so try to avoid such things. A group I play in seemed to have TPKs and near-total kills on such a regular basis that the DM has had trouble even justifying why the latest generation of adventurers are still out there, wandering around on the original quest of which no one PC currently in the group is directly connected to. The campaign arc over the last two years has tied in to the FR Netherill setting, leading in to Forge of Fury, then moving to an old Dragon module set in Myth Drannor, then the Deserts of Desolation series, where everyone remains so far. I lost probaly 21 characters in 2 years (two died when I wasn't even there), and after I missed one session and heard my sorcerer got toasted by a flame strike, I said hell with it, no more for me. Not one character in the tale had any meaningful continuity with the story, and the whole purpose of the adventure had been muddled by the constant party deaths. The DM is a great guy, but he lets the dice fall where they may, and that can be deadly, especially in old school modules with a lot of wandering monsters (which he rolls for religiously).