I believe the saying comes from a superstitious idea that if you wish someone good luck, the opposite will happen; therefore you tell them to "break a leg" so that they will have good luck instead. People involved with theater are said to be very superstitious, although I have not found that to be true, at least at the amateur level
Wikipedia says that this "expression is sometimes used outside the theatre". Could you please list some situations when one can use it?
I have just read a newspaper article about a woman who sent her cousin a "break a leg" text message on his wedding day. He literally broke his leg just minutes before the ceremony - what bad luck!