Deus ex machina
A deus ex machina is an ending or rescue produced by something outside the story's established logic: a power, character or coincidence that nothing preceding it has earned.
The phrase means god from the machine, and it began as real stage machinery. In Greek theatre a crane, the mechane, could lower an actor playing a god onto the stage to settle a plot the characters could not settle themselves. Today the term means any resolution that drops in from outside: the cavalry nobody sent for, the power nobody mentioned, the coincidence that arrives exactly on time.
Where the term comes from
Aristotle objected to the device in the Poetics, on the grounds that the resolution of a plot should come out of the plot itself. His example was Euripides' Medea, in which Medea escapes at the end in a chariot sent by the sun god. Horace, in his Ars Poetica, gave the Roman version of the rule: do not bring a god on stage unless the knot is worthy of a god to untie it.
What it actually means
The most useful thing to understand is that this is a complaint about setup, not about plausibility. A dragon arriving in the final chapter is not a deus ex machina if the book has been about dragons and that particular dragon owes the hero a favour. The fault is arrival without preparation.
The test is one question: who solved it? If the answer is not the protagonist, or a consequence of a choice the protagonist made earlier, the ending will feel hollow no matter how much happens in it.
An example, and a fix
The flood water reached the attic window. Mara had one bar of signal and nobody left to call. Then, over the roar, a helicopter she had never heard of swung into view, and a rope ladder dropped to her feet.
Why it fails: Mara did nothing. The story ends because it was rescued, not because she was.
The fix: in chapter three, Mara talks her neighbour out of selling his fishing boat. In the flood, she swims two streets to the boat. Same rescue, but now it is paid for by her choice.
How to fix one in your draft
There are two honest fixes. Plant the rescue much earlier, so the reader has met it before they need it (see Chekhov's gun and setup and payoff). Or make the protagonist pay a real price to obtain it, so the help is earned even if it comes from outside.
Frequently asked questions
What does deus ex machina mean?
Deus ex machina is Latin for 'god from the machine'. In storytelling it means a problem solved by something the story never prepared for, such as a sudden rescue, a new power or a lucky coincidence, instead of by the characters.
How do you pronounce deus ex machina?
In English it is usually said DAY-us ex MAK-ih-nuh. You will also hear MAH-kee-nuh for the last word, and both are accepted.
Is a deus ex machina always bad?
Almost always, when it resolves the main problem, because it takes the victory away from the protagonist. It can work in comedy or satire, where an absurd rescue is the joke. Luck that gets a character into trouble is fine; luck that gets them out is what feels like cheating.