JessaJessa's answer is great for what to do about the TPK that's already happened (or didn't happen, depending on what option you take).
As for avoiding such a situation in the future, I'd advise reexamining how you build your combats to avoid a setup where one mistake by one player in one round creates an unavoidable TPK. Now, it's entirely possible that the rest of the party got demoralized by that mistake, or didn't adapt in time, in which case, they are partly responsible for the outcome.
As the DM, you have the ability (though not the obligation) to provide more than one avenue for your players to take to resolve a situation. Also, since you control the opponents, you can also adapt your/their strategy on-the-fly to account for changing "battlefield" conditions (incl. non-combat encounters). Admittedly, it can be more involved, especially if you want to do it in such a way that your players feel fairly challenged.
But if you see things going pear-shaped, use your DM's discretion to tweak them back within a range you feel comfortable with. Maybe your PC's get "spanked", coming within a hairsbreath of a TPK, before the orcs move on. Maybe the wizard gets knocked into negative HP for his poor aim, and has a "near-death experience" that could be used as roleplay fodder for his warlord fascination. Whatever suits the situation - you don't have to keep things tightly under control, but you don't have to let them happen as they may either.