The justices upheld lower court rulings which found his move to rescind the Deferred Action for Childhood Arrivals (Daca) programme was "unlawful", the BBC reported.
It protects "Dreamers" - undocumented youths brought to the US as children.
The Trump administration has sought to end the Obama-era policy since 2017.
The Supreme Court took up the case after lower courts ruled that the Trump administration did not adequately explain why it was ending the programme, criticising the White House's "capricious" explanations.
On Thursday, the justices voted 5-4 to uphold the lower courts' findings that the administration's order violated the Administrative Procedure Act, which says a government action cannot be "arbitrary, capricious, an abuse of discretion or otherwise not in accordance with law" or "unsupported by substantial evidence".
The ruling, which does not prevent the Trump administration from continuing in its efforts to end the programme, affects an estimated 700,000 young people who entered the US without documents as children.
Trump denounced the decision in a series of tweets.
"These horrible & politically charged decisions coming out of the Supreme Court are shotgun blasts into the face of people that are proud to call themselves Republicans or Conservative," he wrote. He called on voters to re-elect him in November to put more conservative judges in the court, should there be a vacancy. He also suggested that he would renew efforts to end the programme and "start the process all over again".
Former President Obama praised the ruling and urged voters to elect a Democratic president and Congress in November to ensure "a system that's truly worthy of this nation of immigrants once and for all".
Joe Biden, the expected 2020 Democratic presidential nominee, said he would seek to make the programme permanent should he win the election.
Most of the children protected by the Daca programme are from Mexico and other Latin American countries.
A 2012 executive order, created by former President Obama, shields these so-called "Dreamers" from deportation, and provides work and study permits.
Obama signed the order following failed negotiations for immigration reform on Capitol Hill.
In order to qualify for Daca, applicants under the age of 30 are required to submit personal information to the Department of Homeland Security (DHS), including addresses and phone numbers.
They must go through an FBI background check and have a clean criminal background, and either be in school, recently graduated or have been honourably discharged from the military.
In exchange, the US government agrees to "defer" any action on their immigration status for a period of two years.
It is only available to individuals residing in the US since 2007.
Daca recipients told BBC they were relieved and surprised by the ruling on Thursday, and many said they would continue advocating for immigration reform.
Juana Guzman of Texas, 28, said: "It's a very needed win and this is giving us the fuel we needed to continue moving forward and to keep fighting for the rest of our families and the community that does not have Daca".
Metzli Sanchez, 23, said: "As big of a victory that this is, we have to keep applying pressure that we have to keep fighting for other people who are just as able and capable but who do not have this protection."