District Judge Charles Breyer found the deployment, in response to anti-immigration protests, breached the Posse Comitatus Act of 1878, which prohibits the US military from domestic law enforcement.
The judge said there was no rebellion in Los Angeles and civilian law enforcement was capable of responding to the protests.
The ruling blocks the administration from deploying or using military troops, including the National Guard, for domestic law enforcement activities in California.
The order is paused until 12 September, allowing the Trump administration time to appeal the decision.