The ruling is a rare court victory for Trump's Justice Department in investigations that Trump has demanded.