It has its origins in Western European etiquette, basically the rules of conduct in society.
In a number of situations, it was (and largely still is!) deemed correct behaviour to give ladies precedence over gentlemen. This is for instance the case when serving food or drinks at a table: one servers the ladies, then the other guests, then the host.
It is interesting to note that the common use of ladies first actually often contradicts traditional etiquette:
The phrase is often used to mean after you, but etiquette does not prescribe that ladies should always pass first through a door or other narrow passage. The idea is that the gentleman who accompanies the lady will only let her pass first when entering a trusted environment, such as his or her own home. When entering an unknown place, or when exiting a building, the man is to go first. The reasoning behind this is that if any danger lurks on the other side of the door, the strong man can defend the poor defenceless damsel.
It is also traditionally considered bad form to invite a lady to "go first" when climbing stairs - a situation in which the gentlemen might indecently stare at certain parts of the female anatomy as she moves up and ahead of him...
When in a movie (or in real life) one man tells another "ladies first" instead of "after you", it is a joking insinuation that the other person performs the female role in the relationship between the two men - the speaker is asserting his leadership in the situation.
disclaimer: any references made to women as the weaker sex in need of protection are made solely as historical observation and do not necessarily reflect the personal opinions of the author of this answer!