Deus ex machina is a Latin translation of the Greek apó mechanís theós. In English this should become "god from the machine." The Latin ex is more ambiguous than the Greek apó which should usually not be translated as "out of."
The machine was nothing more than a crane from which an actor in a harness playing a god was lowered onto the stage (skene) at the appropriate moment. It was helpful in improving the realistic representation of divine intervention by having the god him/herself appear at a crucial point of the play to straighten out the plot and put the mortals in their places. It was introduced by Euripides at a date that's not exactly known but at least as early as Medea in 431 BCE and was employed by Greek and Roman playwrights for many centuries as an essential part of their plays.
Until this day, the concept of a god from the machine--an apparently heaven-sent person providing a solution to a quandary--is crucial in literature, theatre and cinema and the technique is widely used. Its use in everyday language as a metaphor for one who shows up to save the day deviates little from the spirit of its original use as a theatrical trick almost 2500 years ago.