...What if it's not an illegal order? Isn't it their job to protect the government?
If the order is counter to the provisions of the US Constitution or the UCMJ, then they are obligated to not obey.
No, it isn't the job of the military to protect the government.
The wordings of the current oath of enlistment and oath for commissioned officers are as follows:
"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. So help me God." (Title 10, US Code; Act of 5 May 1960 replacing the wording first adopted in 1789, with amendment effective 5 October 1962).
"I, _____ (SSAN), having been appointed an officer in the [Army] of the United States, as indicated above in the grade of _____ 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; 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 upon which I am about to enter; So help me God." (DA Form 71, 1 August 1959, for officers.)
(Note that above officers' oath does not include obeying the President or other officers.)
Officers of the National Guard of the various States take an additional oath:
I, [name], do solemnly swear (or affirm) that
I will support and defend the Constitution of the United States and the Constitution of the State (Commonwealth, District, Territory) of ___ against all enemies, foreign and domestic; that I will bear true faith and allegiance to the same; that
I will obey the orders of the President of the United States and the Governor of the State (Commonwealth, District, Territory) of ___, that I make this obligation freely, without any mental reservations or purpose of evasion, and that I will well and faithfully discharge the duties of the Office of [grade] in the Army/Air National Guard of the State (Commonwealth, District, Territory) of ___ upon which I am about to enter, so help me God.
All officers are restricted to
lawful orders as per the Uniform Code of Military Justice (UCMJ).