I was wondering what a soldier is expected to do if his commanders orders him to do something during an operation that would be contrary to Halacha. (For example, I learned that if a truck of explosives was driving towards a crowded marketplace, one would not be able to detonate the truck before it reached the marketplace. Even though it would kill less people, we are not allowed to choose between the values of other people’s lives. A commander might order you to shoot the truck and detonate it because it would kill less people, but this may be considered halachic murder.) Is there any precedence in the halacha for following commanders orders instead of halacha in order to preserve the security of the nation and the command structure of the army?
It is clear that in a military framework a soldier must obey the orders of his commander for the sake of the military operation and to avoid anarchy as long as the order is not contrary to Halachah. A command which is contradictory to Halachah must not be implemented under any circumstances. See Maimonides, Laws of Kings (Chapter 3, 9) that even in the case of a king if he commands to cancel a Mitzvah it is clear that one must not obey him. One who acts in contrary to Halachah, is harming the nation's security and will be judged for it. Regarding your example; it is not clear at all that the Halachah prohibits detonating the truck of explosives, since apparently the law of Rodef (chasing to kill) applies on the driver even if he is doing it unwillingly which in this case it is permissible and one is actually obligated to hurt him in order to save the endangered when there is no other way. We find this law by a pregnant woman where her life is in danger and the only way to save her is by killing the fetus; it is permissible to cut the fetus in her womb by using a drug or by hand because it is considered that he is chasing to kill the mother. See Maimonides, Laws of Murderer 1, 9 and its explanation in Igrot Moshe Yore Deah 2, 60b and Choshen Mishpat 2, 69. It is very important that a soldier engaged in combat learn and find out in advance the opinion of Halachah in cases which may arise during operations, so he should be able to conduct himself according to our holy Torah.