There is no question that war, self-defense, official executions and certain other cases of para-Halachic killings are either sanctioned, required, or otherwise allowed after the fact (and even rewarded).
The questions as to why are not answered in a generality; there has been much discussion about each case, with separate explanations for each.
As to your question relating to the prohibition against murder, the answer is that this is a general rule about taking an innocent life without just cause. {Free translation of link: "Whosoever kills a person among Israel violates a negative commandment, as it is said, 'Do not murder' (Ex. 20:12, Deut. 5:16). And if he murdered intentionally before witnesses, his death is carried out with a blade, as it is said, 'He shall surely be revenged.' (Ex. 21:20) From the implication the sages learned that this is referring to death by blade; regardless of whether he killed his fellow with iron or burned him to death by fire, his death is by blade." -- Maimonides, Laws of A Murderer and Protecting Life, 1:1}