President Donald Trump reacted angrily to Thursday’s U.S. Supreme Court ruling that his administration acted illegally when it ended the Deferred Action for Childhood Arrivals Program (DACA).

The court, in a 5-4 vote, rejected Trump's effort to end legal protections for 650,000 young immigrants, a rebuke to the president in the midst of his reelection campaign.

The justices rejected administration arguments that the 8-year-old program is illegal and that courts have no role to play in reviewing the decision to end DACA.

Chief Justice John Roberts wrote for the court that the administration did not pursue the end of the program properly.

"We do not decide whether DACA or its rescission are sound policies," Roberts wrote. "We address only whether the agency complied with the procedural requirement that it provide a reasoned explanation for its action. Here the agency failed to consider the conspicuous issues of whether to retain forbearance and what if anything to do about the hardship to DACA recipients."

Reaction to the news was immediate on social media, including from former President Barack Obama, whose administration authored the program.

The outcome seems certain to elevate the issue in Trump's campaign, given the anti-immigrant rhetoric of his first presidential run in 2016 and immigration restrictions his administration has imposed since then.

#DACA was one of Twitter’s top trending stories when the court’s decision was announced.

Roberts was joined in the opinion by Justices Ruth Bader Ginsburg, Stephen Breyer and Elena Kagan. Justice Sonia Sotomayor joined the majority in all but one part, and filed an opinion as well.

Justice Clarence Thomas also filed an opinion, concurring and dissenting in part, joined by Justices Samuel Alito and Neil Gorsuch. Justice Brett Kavanaugh filed an opinion concurring and dissenting in part.

The high court heard arguments last fall.

The program grew out of an impasse over a comprehensive immigration bill between Congress and the Obama administration in 2012. President Barack Obama decided to formally protect people from deportation while also allowing them to work legally in the U.S.

But Trump made tough talk on immigration a central part of his campaign and less than eight months after taking office, he announced in September 2017 that he would end DACA.

Immigrants, civil rights groups, universities and Democratic-led states quickly sued, and courts put the administration’s plan on hold.

The Department of Homeland Security has continued to process two-year DACA renewals so that hundreds of thousands of DACA recipients have protections stretching beyond the election and even into 2022.

The Supreme Court fight over DACA played out in a kind of legal slow motion. The administration first wanted the justices to hear and decide the case by June 2018. The justices said no. The Justice Department returned to the court later in 2018, but the justices did nothing for more than seven months before agreeing a year ago to hear arguments. Those took place in November and more than seven months elapsed before the court’s decision.

Thursday's ruling was the second time in two years that Roberts and the liberal justices faulted the administration for the way it went about a policy change. Last year, the court forced the administration to back off a citizenship question on the 2020 census.

The Associated Press contributed to this report.