The ruling came hours after Trump presided over a high-profile White House meeting with lawmakers from both parties on the fate of so-called Dreamers.
Judge William Alsup in San Francisco issued his 49-page ruling ordering the administration to reinstate the Deferred Action for Childhood Arrivals program, known popularly as DACA.
The government is "HEREBY ORDERED AND ENJOINED, pending final judgment herein or other order, to maintain the DACA program on a nationwide basis on the same terms and conditions as were in effect before the rescission," he wrote.
Alsup said the Department of Justice´s view that the program was illegal was based on a "flawed legal premise."
Unless his order is overturned by a higher court, DACA recipients will now be eligible to submit renewal applications and the government will be required to "post reasonable public notice" that the program is once again active.
The so-called Dreamers were protected from deportation under the policy enacted in 2012 during Barack Obama´s presidency.
In September, Trump said he was scrapping the DACA program but delayed enforcement to give Congress six months -- until March -- to craft a lasting solution. ´A bill of love´
The government was sued on grounds that ending the program was arbitrary and done without following proper legal procedures.
Judge Alsup wrote Tuesday that he questioned the administration´s argument that DACA had not been implemented legally.
Alsup said DACA must be resurrected while the legal challenge to it proceeds.
Earlier Tuesday, Trump had taken command of the White House meeting to coax Republican and Democratic lawmakers toward a compromise on the fate of Dreamers.
He also signaled he was open to more comprehensive immigration reform to address millions of other undocumented people living in the shadows, as long as Democrats are willing to countenance greater border security, including a controversial wall along the Mexican border.
"It should be a bill of love," Trump said of a measure under negotiation that would protect hundreds of thousands of Dreamers from deportation.
"But it also has to be a bill where we´re able to secure our border. Drugs are pouring into our country at a record pace. A lot of people are coming in that we can´t have," Trump added, urging lawmakers to "put country before party" and strike a quick solution.
- ´Take the heat´ -
Trump, seated at a long table with some two dozen lawmakers from the House and Senate, presided over the bipartisan talks, allowing journalists rare access to nearly an hour of the meeting.
The president said he would "take the heat" politically if lawmakers were to move toward broader action that would provide a pathway to citizenship for about 11 million undocumented immigrants living in the United States.
"You are not that far away from comprehensive immigration reform," he told Senator Lindsey Graham, after the Republican lawmaker floated the idea of more sweeping legislation.
"You created an opportunity here, Mr President, and you need to close the deal," Graham told him as TV cameras rolled.
Trump´s position appeared at odds with his 2016 campaign, when his platform focused largely on border security and immigration curtailment, and many of his core supporters raged at the prospect of legalizing millions of undocumented immigrants.