Deus ex Machina, or the God out of the Machine. What is it precisely? It's original reference comes from Greek plays. It means the day is saved through an act of God. As a basic example, let us imagine we are in a Greek amphitheater watching a play. The end of the play is coming, and the protagonist is fighting with the antagonist. The antagonist is a better warrior and soon the protagonist is flat on his back with the antagonist's spear hovering over the protagonist's heart. And you think "Ah-ha! This is going to be a tragedy." Suddenly, Zeus appears and strikes down the antagonist, saving the day. If you were to see such a play, you would probably leave the amphitheater feeling unsatisfied.
So what happened? The playwright likely found that he had written himself into a corner and had a time crunch. Thus, he chose a simple resolution. However, the audience was asking themselves "Where did Zeus come from." Zeus appeared only at the end. There was no mention of Zeus anywhere. The protagonist now seems cheapened, since he had nothing to do with the antagonist's defeat.
The advent of author's self-publishing has been a double edged sword. Authors who might otherwise be ignored by swamped publishers because the authors didn't include vampires, zombies, or some other "in thing," have an outlet for their creativity that can provide them with at least some additional income. On the other hand, quite of few of these self published authors have made beginners mistakes. Some of these mistakes are worse than others.
I won't mention names, since I don't want to embarrass anyone. I've read science fiction's novel that began brilliantly. It was exciting. The concept was intriguing and fresh. However, I got to the end, and these aliens came out of no where and saved the day. It was a disappointing ending. In some Christian science fiction novels I've read, an evil immortal enemy is about to win when angles appear and defeat them.
What is the problem? Usually, people expect the protagonist to do something in the end.
So how does the author save the story? One thing they could do is rewrite that part so the antagonist does the work. However, suppose the story is better served is he doesn't? As in the Christian example, it could be a morality tale about trust in something greater than yourself. It can be tricky, but the author's who get away with it best will set it up early. The antagonist could be told that he has to reach a certain spot and perform a certain ritual before the angles come. Perhaps he or she has to find certain relics to perform this ritual. That way, the audience is blindsided. Second, it is established that the protagonist can't do it alone but does need to do certain things himself before he can get help.
I have read many, otherwise excellent novels that fall apart at the end due to the author skipping important steps.