The Supreme Court ruled Thursday against the Trump administration’s effort to end the Obama-era program that offers legal protections to young immigrants brought to the country illegally as children.

The court ruled that the administration's decision to rescind the Deferred Action on Childhood Arrivals (DACA) program violated the Administrative Procedure Act (APA), which sets out rulemaking procedures for federal agencies.

READ THE OPINION

In a 5-4 decision, with Chief Justice John Roberts joining the liberal members to author the opinion, the court said the Department of Homeland Security's move to eliminate the program was done in an "arbitrary and capricious" manner although they did not rule on the merits of the program itself.

"We do not decide whether DACA or its rescission are sound policies. 'The wisdom' of those decisions 'is none of our concern,'" Roberts wrote in his opinion. "We address only whether the agency complied with the procedural requirement that it provide a reasoned explanation for its action."

President Trump quickly reacted to the decision, which came days after an opinion from his own appointee Justice Neil Gorsuch that said employment discrimination based on sexual orientation or gender identity was prohibited under Title VII of the Civil Rights Act.

"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 Conservatives," Trump tweeted Thursday morning, suggesting that the Second Amendment could be at risk if he does not get elected and have the chance to potentially appoint more justices.

Trump then pondered if the recent decisions were personal, asking, "Do you get the impression that the Supreme Court doesn’t like me?"

Former President Barack Obama, meanwhile, hailed the decision, tweeting: "Eight years ago this week, we protected young people who were raised as part of our American family from deportation. Today, I'm happy for them, their families, and all of us."

In Thursday's opinion, Roberts wrote that when the administration rescinded DACA it "failed to consider the conspicuous issues of whether to retain forbearance" -- referring to the non-enforcement of immigration laws to remove those with DACA protection -- as well as the impact the decision would have on DACA recipients who have relied on the program.

"That dual failure raises doubts about whether the agency appreciated the scope of its discretion or exercised that discretion in a reasonable manner," Roberts wrote, noting that the administration could have scrapped the benefits provided by DACA while keeping the non-enforcement policy, but instead eliminated all of it without even giving a reason for ceasing non-enforcement.

"The appropriate recourse is therefore to remand to DHS so that it may consider the problem anew."

GRAHAM SAYS TRUMP 'RECEPTIVE' TO DACA PACKAGE AFTER MEETING WITH GOP SENATORS

The program was established through an executive order from Obama that the Trump administration argued was improper to begin with, claiming this should have been done via legislation from Congress.

Roberts made clear that the administration does indeed have the power to rescind DACA, just not in this fashion.

“The dispute before the Court is not whether DHS may rescind DACA. All parties agree that it may," the chief justice wrote. "The dispute is instead primarily about the procedure the agency followed in doing so.”

The Trump administration had argued that the decision to eliminate DACA does not fall under the purview of the APA because DACA itself was merely a decision not to enforce existing law against a certain group of people. The Supreme Court disagreed, noting that "DACA is not simply a non-enforcement policy" because it is an actual program where people apply to receive a benefit.

"In short, the DACA Memorandum does not announce a passive non-enforcement policy; it created a program for conferring affirmative immigration relief," Roberts wrote. "The creation of that program—and its rescission—is an 'action [that] provides a focus for judicial review.'"

The court also disagreed with the administration's position that the Immigration and Naturalization Act prohibits the decision to rescind DACA from being reviewed by the courts.

SOLICITOR GENERAL NOEL FRANCISCO, WHO HAS ARGUED MANY TRUMP POSITIONS BEFORE SUPREME COURT, LEAVING DOJ

Justice Clarence Thomas wrote in a dissenting opinion that the decision to repeal DACA was correct, and that the majority is holding the Trump administration's decision to a higher standard than the order that established DACA in the first place.

"These cases could—and should—have ended with a determination that his legal conclusion was correct," Thomas wrote. "Instead, the majority today concludes that DHS was required to do far more."

He continued: "Without grounding its position in either the APA or precedent, the majority declares that DHS was required to overlook DACA’s obvious legal deficiencies and provide additional policy reasons and justifications before restoring the rule of law. This holding is incorrect, and it will hamstring all future agency attempts to undo actions that exceed statutory authority. I would therefore reverse the judgments below and remand with instructions to dissolve the nationwide injunctions.”

Thomas accused the court's majority of wanting to avoid making waves, even it if meant getting the law wrong.

In a line that Trump retweeted Thursday morning, Thomas said, "Today’s decision must be recognized for what it is: an effort to avoid a politically controversial but legally correct decision."

At issue was a program created under executive order that gave about 700,000 people brought to the United States illegally as children the opportunity to receive a renewable two-year period of deferred action from deportation and become eligible for a work permit.

Hundreds of "Dreamers" and their supporters had rallied outside the court during the November oral arguments. Some carried signs, such as "Build Bridges, Not Walls." Trump at times seemed to be pressing instead for a legislative solution, and had tweeted earlier that morning that if the administration prevailed, "a deal will be made with the Dems for them to stay!"

The Trump administration announced its plan to phase out the program in 2017, only for the federal courts to rule that it could not apply retroactively and that DACA should be restarted in full. The White House fought back on those decisions, saying the president has broad authority over immigration enforcement policy.

The Justice Department had argued the DACA program is not working and is unlawful, and that the president should have the "absolute discretion" to adopt a revised overall immigration strategy. A dozen states led by Texas were among the parties backing the administration.

DACA proponents have argued that Trump’s planned termination violates federal law requiring adequate notice-and-comment periods before certain federal rules are changed, as well as other constitutional equal protection and due process guarantees.

CLICK HERE TO GET THE FOX NEWS APP

They say the government has given only cursory explanations for justifying DACA's demise. By contrast, they say, the economic and social benefits for Dreamers and for the country are indisputable.

A host of civil rights groups filed separate briefs in support, along with several states including New York and California.