Any commissioned officer who uses contemptuous words against the President, the Vice President, Congress, the Secretary of Defense, the Secretary of a military department, the Secretary of Transportation, or the Governor or legislature of any State, Territory, Commonwealth, or possession in which he is on duty or present shall be punished as a court-martial may direct.
The basic principle here is civilian control of the military. A 2001 article from the American Forces Press Service put it this way:
The country survived the Civil War with the idea of civilian control of the military still intact. The military shrank in size and was mostly in the West. Military officers shied away from politics and many even refused to vote, feeling that this would somehow influence their service.
This held true through World War II. There was such separation that after World War II, Democratic President Harry S. Truman offered to give the Democratic nomination for president in 1948 to General of the Army Dwight D. Eisenhower. Ike, a graduate of West Point, had never voted. People did not know his party affiliation. He turned down Truman's offer, but in 1952 did run for president -- as a Republican.
Today, service members of all ranks are encouraged to vote. The military vote in Florida in this past election was crucial. Once they vote, however, soldiers, sailors, airmen, Marines and Coast Guardsmen are expected to forget their party affiliations and follow the orders of the civilian leaders regardless of the party.
Military members swear "to support and defend the Constitution of the United States." One of the more successful aspects of that document is civilian control of the military.