I do solemnly swear (or affirm) that I will support and defend the Constitution of the United States against all enemies, foreign and domestic; that I will bear true faith and allegiance to the same; and that I will obey the orders of the President of the United States and the orders of the officers appointed over me, according to regulations and the Uniform Code of Military Justice.
National Guard enlisted members take a similar oath, except they also swear to obey the orders of the Governor of their state.
Officers, upon commission, swear to the following:
I do solemnly swear that I will support and defend the Constitution of the United States against all enemies, foreign and domestic; that I will bear true faith and allegiance to the same; that I take this obligation freely, without any mental reservation or purpose of evasion; and that I will well and faithfully discharge the duties of the office on which I am about to enter.
Military discipline and effectiveness is built on the foundation of obedience to orders. Recruits are taught to obey, immediately and without question, orders from their superiors, right from day-one of boot camp.
Military members who fail to obey the lawful orders of their superiors risk serious consequences. Article 90 of the Uniform Code of Military Justice (UCMJ) makes it a crime for a military member to WILLFULLY disobey a superior commissioned officer. Article 91 makes it a crime to WILLFULLY disobey a superior Noncommissioned or Warrant Officer. Article 92 makes it a crime to disobey any lawful order (the disobedience does not have to be "willful" under this article).
In fact, under Article 90, during times of war, a military member who willfully disobeys a superior commissioned officer can be sentenced to death.
Seems like pretty good motivation to obey any order you're given, right? Nope. These articles require the obedience of LAWFUL orders. An order which is unlawful not only does not need to be obeyed, but obeying such an order can result in criminal prosecution of the one who obeys it. Military courts have long held that military members are accountable for their actions even while following orders -- if the order was illegal.
"I was only following orders," has been unsuccessfully used as a legal defense in hundreds of cases (probably most notably by Nazi leaders at the Nuremberg tribunals following World War II). The defense didn't work for them, nor has it worked in hundreds of cases since.
The first recorded case of a United States Military officer using the "I was only following orders" defense dates back to 1799. During the War with France, Congress passed a law making it permissible to seize ships bound to any French Port. However, when President John Adams wrote the order to authorize the U.S. Navy to do so, he wrote that Navy ships were authorized to seize any vessel bound for a French port, or traveling from a French port. Pursuant to the President's instructions, a U.S. Navy captain seized a Danish Ship (the Flying Fish), which was en route from a French Port. The owners of the ship sued the Navy captain in U.S. maritime court for trespass. They won, and the United States Supreme Court upheld the decision. The U.S. Supreme Court held that Navy commanders "act at their own peril" when obeying presidential orders when such orders are illegal.
The Vietnam War presented the United States military courts with more cases of the "I was only following orders" defense than any previous conflict. The decisions during these cases reaffirmed that following manifestly illegal orders is not a viable defense from criminal prosecution. In United States v. Keenan, the accused (Keenan) was found guilty of murder after he obeyed in order to shoot and kill an elderly Vietnamese citizen. The Court of Military Appeals held that "the justification for acts done pursuant to orders does not exist if the order was of such a nature that a man of ordinary sense and understanding would know it to be illegal." (Interestingly, the soldier who gave Keenan the order, Corporal Luczko, was acquitted by reason of insanity).