"Eucatastrophe" is a term coined by J.R.R. Tolkien in one of his letters in 1944, and a concept used in essential parts of his book The Lord of the Rings. His primary explanation of the word is given in his essay "On Fairy-Stories":
But the 'consolation' of fairy-tales has another aspect than the imaginative satisfaction of ancient desires. Far more important is the Consolation of the Happy Ending. Almost I would venture to assert that all complete fairy-stories must have it. At least I would say that Tragedy is the true form of Drama, its highest function; but the opposite is true of Fairy-story. Since we do not appear to possess a word that expresses this opposite — I will call it Eucatastrophe. The eucatastrophic tale is the true form of fairy-tale, and its highest function.
This means that whereas a catastrophe is a sudden, terrible turn from good to tragedy, a eucatastrophe is the opposite; a turn from a terrible and certainly dreadful moment of defeat to a sudden and beautiful victory.
Etymology
Eu (ευ-) is a common Greek term meaning "good" (this also appears in words such as euphoric). Catastrophe is an English word meaning "sudden and unexpected disaster", from Greek καταστροφή, 'destruction'. And so eucatastrophe, combining these two, is a noun referring to "a sudden and unexpected good or happy ending", as the antonym to catastrophe.
Inspiration
The idea of a sudden and unexpected "happy ending" coming out of bad was drawn from Tolkien's Catholic faith, for he called the Incarnation of Christ the eucatastrophe of "human history" and Christ's resurrection the "eucatastrophe of the Incarnation."
The design of "Eucatastrophe" is found twice in The Return of the King: It first occurs when Aragorn, who had been thought not to return from the Paths of the Dead, suddenly appeared with the Grey Company out of the Black Ships, at the very moment the Corsairs were instead expected to arrive and complete Mordor's defeat of Minas Tirith. This turns the tide of the battle, and the Enemy's hope is lost. It happens again when the One Ring is at last destroyed by Gollum at the Cracks of Doom, in spite of Frodo's failure to relinquish it, which brought a sudden end to all evil.