In a movie, when the plot is really complex and then something ridiculous comes in at the end and solves everything, that ridiculous thing is a deus ex machina, an unlikely solution to an impossible situation.
Deus ex machina is Latin for “god from a machine,” and comes from Greek plays where actors playing gods would descend from above and tidy up the mess that humans made. For example, in the play Medea, the title character kills a king, a princess, and her own two children, and just before being captured, her grandfather enters on a golden chariot pulled by dragons and rescues her. It's a narrow escape that only a deus ex machina could provide.