The second amendment provides the citizens of the United States the right to bear arms. While there have been arguments over whether or not the intention of the law was to provide all citizens with arms and of what type, the basic interpretation of the law is that any citizen in good standing (ie. one who can pass a background check and who meets the legal necessities of the state) is allowed to have a firearm. However, even if you're allowed to own a firearm there are even more laws that state when, where and under what circumstances you're allowed to use said firearm. Fortunately in some states there's a legal rule called the Castle Doctrine, and it takes all the guess work out of home protection even, or especially, concerning the use of lethal force.
Castle Doctrine is based off the saying that, "a man's home is his castle." What this means is that, according to the Castle Doctrine, you as the homeowner have no duty to retreat in your own home. Duty to retreat is another legal term that means you should only use force as a last resort. Say that you were going to be mugged and you were armed, but instead of running away you chose to shoot your mugger. Some states would penalize you legally for this decision because you could have run away and brought no harm to anyone. This applies even in your own home if you're in a state that doesn't have a Castle Doctrine, or something similar. In states that do support the Castle Doctrine though, if you are at home and someone breaks into your house then you as the homeowner have the right under the Castle Doctrine to defend your home using the same force brought against you. This applies even if you had the ability to run away to safety by fleeing from your home.
It should be clear though that Castle Doctrine only applies when you're in your home, although home could be a family home, an apartment complex or in some stretches of legal imagination a rented hotel room that you're staying in. This legal protection doesn't extend out to other situation. The basis of the legal idea is that, because you're in a place that should be safe you shouldn't have to run away from your safe haven in order to preserve yourself. It's also important to note that even though firearms are mentioned most often in this article that other weapons, both lethal and less than lethal weapons, are also covered under the Castle Doctrine.