The ruling marks a major legal win for Trump after labor unions sought to block mass terminations and other actions aimed at shrinking the government.