The short answer is - the people receiving orders either obey them or refuse, and suffer the consequences. If sufficiently few people choose to obey an order, or deem it unlawful, then chaos ensures and someone ends up in charge. It could be akin to a Saturday Night Massacre, where 2 AG’s were fired before Bork accepted and fired the special prosecutor. However, it had severe political repercussions, for Nixon and much later, for Bork. Something similar almost happened to Trump.
OTOH, if the person refusing is not backed by a decent number of the people behind him, it’s basically career suicide. best case, they get to resign. Worst case, they do time for mutiny. Because obviously, if the top general says “no” the prez will say “you’re fired” (unless those words are trademarked) and say the next guy in line is promoted. Then it’s the next guy’s turn to decide. Or the prez appoints his favourite pet military guy, and the ranks then choose wheter to accept that or not.
(As a side note, in recent demonstrations about decapitating the Israeli justice system, a large number of military and reserves threatened to no-show if the measure went through)
You might also end up with a standoff where the president says “do it” and the great majority of the generals say “nope”. The solution to this is political. If it’s a prelude to the president being told by almost everyone to back down, or quit (as happened to Nixon) then it just helps trigger the situation. Otherwise, the president cleans house. Such a standoff cannot last very long.
The previous responses are spot on - there is no magic means by which someone embedded in the command chain can second guess or ignore the formal processes that produce a political leader. It can only succeed if it is part of a massive reaction and much of society refuses to accept the result.
(The Shah had to exit Iran in a hurry, for example, when he told the army to fire on rioting civilians and they - many draftees - refused to obey. The Russian government collapsed when the soldiers en masse refused to obey the generals. That’s the alternative to accepting results.)