If you've written a story or two, you're probably guilty of using it. I know I am. I hate it, and try to avoid it at all costs, but sometimes it happens. It's called deus ex machina, Latin for "god out of the machine", and it's a plot device used all too often to solve an issue or problem the author just can't quite wrap up the way he or she desires. We see it all the time in books and movies. We get right to the end of the story, and in order to resolve the issues in a satisfying manner, some kind of new event or character needs to intervene and set things right. Who the hell is this guy, and where did he come from to save the day just in the nick of time?
Shakespeare used it in several of his plays. Many other famous and talented writers have used it. It's not just reserved for hacks and less talented writers. It is common, though, and employed by both the talented and hacks alike. In fact, it's probably more common than you might think, especially in today's Hollywood, where plot takes a backseat to special effects and viewership numbers.
It's been called pure laziness. Shoddy writing. Personally I can't stand it, and heckle it any chance I get, even if I am guilty of using it a time or two. To me it signifies an author hasn't spent enough time crafting the story to a better outcome. That the author doesn't care enough about the story to fix those issues correctly, or let the story end how it will. The characters should determine the outcome of the story, not the author.
I think the problem stems largely from our desire to have a happy ending. Nobody likes coming to the end of a good tale and finding out the characters they invested so much emotion in during the story die at the end. Nobody likes being slapped with a healthy dose of reality after reading or watching a fantastic tale that allowed them to escape that brutal reality for a while. It's inherent in who we are as humans, and it's probably never going to change.
So aside from giving in and settling on an ending that nobody's going to like because your characters got in over their heads, how does one avoid using deus ex machina as plot spackle to make it all better? If I knew that answer, I'd probably be a lot better off than I am now. That doesn't mean I don't have a few thoughts about how to avoid it, though.
The easiest way, as I mentioned earlier, is to just let the plot work out the way the characters make it. It doesn't always end happily that way, but then, neither does life. Sometimes people die, or get hurt, or get in trouble over their heads. Sometimes things don't turn out the way we planned them to. That's just the way life is. It's not the sexy ending, but it is realistic.
But it doesn't have to be that way. Your characters are resourceful. They're well rounded and experienced. They've probably got the answer somewhere. You just have to get it out of them. Experiment with them a little. Tweak their reactions to something a bit. Don't just go with the first reaction they might have and write it down. Follow one reaction to its logical conclusion and switch it up a bit. Chances are, you'll find some kind of combination that allows the conflict and tension you need in the story and the resolution you need to wrap it all up with a happy ending.
The stakes don't need to be all that high either. We see more and more stories where all-or-nothing scenarios are played out. The hero must save the world from imminent destruction in order to set things right, save the girl and live happily ever after. Or at least until the sequel. The problem with these types of scenarios is that they don't leave a lot of wiggle room for the writer. If it's an all-or-nothing problem, the character really doesn't have a choice to make, meaning that the reader should already be well ahead of the game of figuring out how things are going to end. If the reader has the ending figured out, the story either ends predictably, which is lame, or it has an unexpected twist. And if the twist isn't set up right at the beginning, enter deus ex machina to save the day.
The same holds true for the fates of your characters. If the hero and villain are put together in such a way that only one will walk away alive, there again isn't much doubt about how it's going to end. Creating a storyline and characters who can interact with each other and live to tell about it sets up future conflict. It also mitigates the need to make more and more bad guys to throw at the good guy as the story progresses.
If the stakes aren't as high, there are probably a number of ways the situation can be resolved, meaning there are far more options to the author, and greater suspense for the reader. Yes or no questions pose an ultimatum, and create a situation with only one feasible ending. By using open ended questions for plot construction, you've given the characters more ways to interact and get themselves back out of the pickle they've gotten themselves into.
But what if you really want the stakes that high? After all, it's more exciting when there's more at stake in the story. Giving the readers a good apocalyptic tale does have its advantages. And you can still do that, if you set up the rules of engagement in the future to allow multiple possible happy endings. You've got to delve into the plot more than just superficially. You've got to spend some time running decisions in the story to their logical conclusions. It takes a lot of hard work and careful thought to come up with a good plot that follows through clear to the end. But doing so is rewarding. If you're willing to spend the extra effort to avoid letting deus ex machina save the day for you, your story will probably be a whole lot better.