The ruling came hours after Trump presided at a meeting with rival US lawmakers on the fate of so-called "Dreamers."
Judge William Alsup ordered the administration to reinstate the Deferred Action for Childhood Arrivals program, saying a Department of Justice view the program was illegal was based on a "flawed legal premise."
Unless Alsup's order is overturned by a higher court, DACA recipients can submit renewal applications. Dreamers were protected from deportation under a policy set in 2012 during Barack Obama's presidency. But in September Trump said he was scrapping the program.
Hours before Alsup's decision, Trump hosted a meeting to coax Republican and Democratic lawmakers toward a deal on the Dreamers.
He indicated he was open to comprehensive immigration reform including a pathway to citizenship to address about 11 million undocumented people if Democrats backed greater security, including a new barrier along the US-Mexican border.