In its simplest form, military necessity is a part of just war theory, specifically dealing with jus in bello, or the conduct of combat. In U.S. and NATO usage, it is phrased as position that a belligerent has the right to apply any measures which are required to bring about the successful conclusion of a military operation and which are not forbidden by the laws of war.
The laws of war, however, are not black and white. Their most immediate problem is that all belligerents may not comply with the same set of written or customary laws, and even written international law does not cover all situations. For example, the Geneva Conventions principally assume the belligerents are nation-states. In a modern military environment, if an insurgent force fighting an advanced opponent followed the rule of wearing distinctive insignia distinguishable at a distance, the other side's sensors could detect them and kill them at ranges beyond ranges of their own.
A given culture, such as the Japanese in the Second World War, may not assume that its own personnel will surrender, or that its opponents will not fight to the death. While the Japanese treated Allied prisoners savagely, they had simply not planned for handling prisoners.
There are many situations in which a regular combatant unit cannot take prisoners. Submarines survive by stealth; revealing themselves can make them vulnerable to attack. Further, they rarely have the physical space to take prisoners or keep them secure. By the middle of the Second World War, both sides sank vessels of the other without attempting to assist survivors. In some cases, if the survivors were soldiers and had a reasonable chance of returning to battle, the submarine might surface and machine-gun them.
In like manner, an infantry patrol behind enemy lines cannot maintain its security if it takes prisoners with it. There are no clear answers; do they kill all, let them go free, or tie them up in hope that they are found before they starve?
Prevention detention of persons judged security risks also can be justified on grounds of military necessity, as with the internment of aliens and citizens of Japanese ancestry on the West Coast of the U.S. in WWII — some relatives of those same individuals, living in Hawaii, volunteered for U.S. military service in 442nd Regimental Combat Team, an extremely valorous unit.
Military necessity has been used as the authority for both enhanced interrogation techniques that attempt to stay within international law, as well as outright torture for potential "enemies of the state" with critical information.
One of the doctrines of jus in bello is proportionality; the civilian and cultural damage caused by an attack on a military objective is expected to be no more than needed to achieve a military objective. Some have considered this a general ban against nuclear weapons, but if the weapons were used in a counterforce targeting of isolated bases, would a strict prohibition apply? Different criteria are involved when Second World War bombers could not hit more precisely than a part of a city, as opposed to precision-guided munitions capable of hitting within a few meters of the target.