The worst thing you could possibly do is cop out with a deux ex machina, but as for ways around it...if you dig the hole deep enough, you have to go back and create a way out. I will say that sometimes you have to out-think the villain, using brains and wit instead of just cutting their head off with a sword. If it fits the style or characters, that's one of my favorite ways around a deus ex. Also, sometimes you have to rely on the villain's ability to kill or whatever their strength is and use that to your advantage. If they're physically better, your main character can't walk in expecting to beat them so he/she has to plan for after they get beaten. If you create a good enough villain, though, a happy ending is incredible hard to get with any realism. Assuming you want the big showdown to end with the good guy alive, of course. Otherwise, go out with a bang.
Hope this helps.