For example, if the president ordered the military to launch nukes to attack a country that poses no real threat, assuming its legal, can the military refuse to follow through on it on the grounds that it is immoral?
The Uniform Code of Military Justice (articles 90-92) state that a service member will be subject to court martial if he or she "willfully disobeys a lawful command of that person’s superior commissioned officer." The operative word is here is 'lawful' which must be explicitly interpreted during court martial proceedings. Any service member who has a reason to believe that an order is given against the interests of the United States, in violation of military rules, oaths, or procedures, or otherwise improper or illegal, maintains the right to refuse to comply. It's a risk that a soldier might take on conscience, and like all risks of conscience it may result in prosecution. But that's what courts are for: to determine whether actions are lawful and legitimate.
Unknown, but unlikely.
1973, during the end of Nixon's reign, Harold Hering, twenty-one years into his Air Force career, while serving as a Minuteman missile crewman and expecting a promotion to lieutenant colonel, posed the following question during training at Vandenberg Air Force Base:
"How can I know that an order I receive to launch my missiles came from a sane president?".
This question led to his eventual administrative discharge. So at least in 1973 missile crewmen were expected to not question their orders.
House Majority Leader Nancy Pelosi actually got an answer to this question, and it backs up Ted Wrigley's answer.
This comes from Gen. John Hyten, "now the nation's No. 2 military officer". Notice that this is a 4 star General, not an enlisted soldier or even a lower officer, that would be making the decision of what order is legal or not. (FYI, a 4 star general is the highest military peacetime rank. The 5 star general is for wartime only.)
“And if it’s illegal, guess what’s going to happen? I’m going to say, ‘Mr. President, that’s illegal.’ And guess what he’s going to do? He’s going to say, ‘What would be legal?’ And we’ll come up with options, of a mix of capabilities to respond to whatever the situation is, and that’s the way it works. It’s not that complicated.”
"There is no mythical red button. That mythical button doesn’t exist. That is a Hollywood thing,” added a senior military official who was not authorized to speak publicly. “It is a very complicated process. We have legal advisers in the room on both sides — on the military side and on the civilian side. The laws of armed conflict, the law of war, play a big role in this — ethical proportionality and all that.”
...a Defense Department official who advises nuclear commanders ... “The way the nuclear arsenal is employed, it is not that easy for a crazy president to go and launch nukes,” the official said. “That’s kind of a silly thing to say.”
A senior military official stressed on Friday that troops have been taught to recognize and disobey illegal orders, particularly those at U.S. Strategic Command, which oversees America’s nuclear arsenal. While theoretically Trump could fire any officer who refused to obey an illegal order, he would be hard-pressed to find another who would implement it, the person said.
“It is important that our oath is to the Constitution, not to a person,” the person said. “I’m not saying that is easy, but I’m still confident that we would be able to do it.”
This is essentially confirmed by AP News.
Although it would be unprecedented, a military officer could refuse to obey a president’s order to launch a nuclear weapon if a legal assessment concluded that it constituted an illegal act under the internationally recognized laws of armed conflict. This is a murky area, given that the circumstance has never arisen.
“If the military gets an illegitimate order from the president of the United States, the military can and should refuse that order in a situation where it is widely seen that the president is unfit and incapable of making a rational decision,” said Tom Z. Collina, co-author with former Defense Secretary William J. Perry of a book, “The Button,” about nuclear dangers and presidential command authority.
Under existing procedures, a president who was considering the need to use nuclear weapons would be expected to consult with advisers, most likely to include the secretary of defense, the chairman of the Joint Chiefs of Staff, and the commander of U.S. Strategic Command, which has operational control over the nuclear arsenal. Various assessments would be made, including the legal aspects of strike options.
The current arrangement in which sole authority to order a nuclear launch rests with the president is not written in law. It was created by Truman as a means to keep decisions about use of the world’s most dangerous weapons in civilian rather than military hands. The authority is considered inherent in a president’s constitutional role as commander in chief.
Also in November 2017, the Air Force general who was commanding Strategic Command at the time raised the possibility of having to refuse an illegal launch order. That officer, Gen. John Hyten, who is now the vice chairman of the Joint Chiefs of Staff, stressed that in any circumstance the military is obliged to only follow legal orders.
“I provide advice to the president,” Hyten said. “He’ll tell me what to do, and if it’s illegal, guess what’s going to happen? I’m gonna say, ‘Mr. President, that’s illegal.’ Guess what he’s going to do? He’s going to say, ‘What would be legal?’ And we’ll come up with options of a mix of capabilities to respond to whatever the situation is, and that’s the way it works. It’s not that complicated.”
Not under the laws of the USA, but under international law.
The most famous example of this was with the Nuremberg trials, where leading Nazis were tried (and a number executed) for war crimes and crimes against humanity. Whilst some international laws existed relating to this, this was the first time that military and civil leaders had been brought to trial for war crimes, and the concept of "crimes against humanity" was explicitly created to deal with the creators of the Holocaust.
During the remainder of the 20th century, a number of Nazis involved in the Holocaust were also captured and tried. This was generally on a fairly ad-hoc basis, and little was done to deal with ongoing world problems.
Then the Balkans conflict happened. In the aftermath of that, the International Criminal Court was established as a permanent, supernational court to deal with war crimes and crimes against humanity. This happens basically without fear or favour. It does not try to replace due process in countries which already prosecute offenders effectively. Instead it explicitly exists to prosecute offenders where the country concerned has made it legal to carry out war crimes or crimes against humanity.
The requirement for this was clear from the Holocaust, and this is one case where Godwin's Law doesn't apply. Every death in the Holocaust was legal under German law. The Nuremberg trials established very clearly that "just following orders" was no defence; civil and military authorities were required to exercise moral judgement and refuse to follow orders which broke international law. If anyone thinks national laws should take precedence over international law, they are in favour of the Holocaust having happened. It really is that simple.
You may think the ICC would be pro-American. Ironically that isn't the case, because the US really doesn't like outside criticism of its military. The ICC is already investigating the US for war crimes in Afghanistan; the US has placed sanctions on the investigators, for no more reason than the fact that they are investigating US actions, but the investigation is still taking place. Based on this, if the US commits other war crimes such as an unprovoked nuclear strike against civilian targets, it is clear that the ICC would pursue action in that case too.