"Break a leg" is not a pun or a metaphor. Unless the person saying it is really talking about breaking a leg, it's an idiom.
A "pun" is a kind of joke that relies on words with different meanings sounding similar. You say something that would make sense if you used word X, but then you use word Y instead. Y is a word that sounds the same as X, or that sounds like X, but means something different. For example, "The science teacher told his class a chemistry joke, but he didn't get a reaction." To ruin the joke by explaining it: "reaction" can mean what people do after you do something. So the writer is saying that the students didn't react to the joke, that is, they didn't laugh. But "reaction" is also a term in chemistry for when two chemicals mix and there is a chemical change. The joke plays on these two different definitions of "reaction".
Or, "When my friend learned that I was an avid bird watcher, he started making all sorts of dumb bird puns. But toucan play at that game." "Toucan" is a kind of bird, but it sounds like "two can", "two can play at that game".
A "metaphor" is when you compare two things, but you say it like it is a fact and not a comparison. Like, "The weight lifter has muscles of iron." His muscles aren't really made of iron, of course, but they are so strong they are like iron.
By the way, when you do use comparison words, such as "like", we call it a "simile". So "His muscles were like iron" is a simile. "His muscles were iron" is a metaphor.
"Break a leg" is an "idiom". That means a word or phrase that has a meaning other than the literal meaning. It is common to tell someone who is about to try something difficult, "Hey, break a leg", which means "good luck". In some ways it's a funny idiom, as it means almost the opposite of the literal words. If someone was attempting something dangerous and you said "break a leg", you mean that you are hoping that he does not break his leg.
Of course "break a leg" can also be said literally. Like, "If someone steps in this hole and trips, he may break a leg." There we mean to literally break the bones in his leg.
So after all that, how can you know if a statement is meant literally or as an idiom? You can only tell from context, from the words that come before and after, and what is happenning in general. If someone is speaking the words, facial expressions and tone of voice may also be a clue.
For the phrase "break a leg", if you are heading into a job interview for a job you are hoping to get, and a friend says, "Hey, break a leg!", he almost surely means the idiom, "good luck". If you are having a vicious argument with your ex-wife and as you leave she screams, with her face red and her eyes bulging, "I hope you fall down the stairs and break your leg!", she probably means it literally.
If you are familiar with the idiom, it's usually easy to tell from context whether the literal or idiomatic meaning is meant. People do get confused someteimes.
The more serious problem for someone learning the language is that you might not know the idiom, and so you try to understand the words literally. Many idioms have little to do with the literal meaning of the words. For example, "kick the bucket" is an idiom meaning "die", as in, "My father kicked the bucket last year" means that he died last year. But if you don't know the idiom and you heard someone say, "My father kicked the bucket", you might wonder what bucket the person was talking about, why his father kicked it, and what kicking a bucket has to do with what you were talking about. I suppose that if taking the words literally doesn't make any sense, maybe it is an unfamiliar idiom. But if you're just learning the language, it could be that you are confused about the literal meanings of the words.
Maybe the short answer is: There's no easy answer. It's not like idioms all start with the letter "b" or any simple rule like that. You have to learn them one by one.