I received this question today via Quora. My answer:
Red and green, and also white, were chosen for railway signaling in Britain in the first half of the 19th century The signals at the time were essentially oil lamps inside a metal housing with a glass window. And since because good quality glass could be had in those colors (white = clear), they became the standard colors.
Since the British were at the forefront of the Industrial Revolution, the choice that they made was adopted as the standard in many other countries as well.