“Break a leg” is usually said to a performer (actor, musician etc.) before a performance.
However, it is now common to use the expression in other situations. (Ex: taking a test)
The origin of this phrase is unknown. There are many different stories of how it originated.
In theatrical performances, it is somewhat superstitious to say “good luck” because it is believed saying it will only cause bad luck for the performer. So saying something negative like “break a leg” will cause a good luck.
Ex: I know you will do a great job. “Break a leg!”
Ex: As the woman was about to go on stage to perform, her friend told her to “break a leg!”