Washington (CNN) Chief Justice John Roberts cast the deciding vote against President Donald Trump's attempt to add a citizenship question to the 2020 census, but only after changing his position behind the scenes, sources familiar with the private Supreme Court deliberations tell CNN.

The case was fraught with political consequences. Democrats and civil rights advocates claimed the query would discourage responses to the decennial questionnaire from new immigrants and minorities and affect the balance of power nationwide.

Roberts' action recalled his dramatic switch in the 2012 case that saved President Barack Obama's Affordable Care Act. Once again, the chief, an appointee of President George W. Bush and a reliable conservative, had sided with the liberals as a dispute of immense national significance went down to the wire.

More broadly, his moves in the census dispute demonstrate that as he begins his 15th year as chief justice, Roberts has become less predictable. He is wearing the heavy mantle of a vote at the middle of a divided bench in this new chapter of his tenure, with the 2018 retirement of centrist-conservative Justice Anthony Kennedy and a solid 5-4 conservative majority.

For the most part, Roberts' opinion in the census case laid out why Commerce Secretary Wilbur Ross had significant latitude to add a new question. He was joined by his four conservative brethren on that point. But then the chief justice swerved, and joined by the four liberal justices, said Ross' justification for the citizenship question, tied to enforcing the Voting Rights Act, was contrived.

After the justices heard arguments in late April, Roberts was ready to rule for Ross and the administration. But sometime in the weeks that followed, sources said, Roberts began to waver. He began to believe that Ross' rationale for the citizenship question had been invented, and that, despite the deference he would normally give an executive branch official, Ross' claim had to matter in the court's final judgment, which Roberts announced on June 27

Roberts' action in the case adds a new dimension to the question that has hung over Trump's presidency and is especially at the forefront as the justices begin a new annual session in October: Will America's highest court restrain the administration's most controversial policies?

On the whole, the 64-year-old Roberts has given the Trump administration great leeway. A year ago, he accepted its arguments and wrote the court's opinion upholding Trump's travel ban. And earlier this year, Roberts voted to allow the administration to ban transgender people from serving in the military and allow Trump to use military funds to build a border wall. On Wednesday night, he let drastic limits on Central Americans seeking asylum at the southern border take effect

Yet on the census, Roberts turned against the administration.

Unlike the other branches of government, the Supreme Court is highly secretive and justices and their staff rarely disclose what they talk about behind closed doors. These developments are reported here for the first time, but much is still unknown about the private deliberations. Roberts declined to comment on this report.

Passions on the citizenship question

In proposing to add the citizenship question to the full census, Ross insisted that it was needed because the Justice Department needed citizenship data to enforce the 1965 Voting Rights Act.

Multiple cities, states and civil rights groups sued, saying that the question would decrease the response rate of new immigrants and, because the count is used to apportion seats for the US House of Representatives and statehouses, undercut political representation for mainly urban, heavily immigrant locales in America. The figures also determine how funding for a multitude of federal programs is distributed nationwide.

After a trial in New York, US District Court Judge Jesse Furman ruled that Ross, among other transgressions, had violated the Administrative Procedure Act's prohibition on "arbitrary and capricious" agency choices.

Furman said the evidence showed that soon after Ross took office in early 2017, he discussed the addition of the citizenship question with then-White House adviser Steve Bannon. Furman found that Ross had sought to add the citizenship question independent of the DOJ request, which he had solicited to back up his plan.

"[P]erhaps most egregiously," Furman said in an opinion that ran 277 pages , "the evidence is clear that Secretary Ross's rationale was pretextual — that is, that the real reason for his decision was something other than the sole reason he put forward in his Memorandum, namely enhancement of DOJ's VRA enforcement efforts."

The Trump administration appealed Furman's decision directly to the Supreme Court, saying the matter was urgent because census forms would have to be printed by summer.

During oral arguments on April 23 , Roberts suggested there were grounds for adding the citizenship question that had not been asked of all households since 1950.

"We've had demographic questions on the census -- I don't know how far back, but, certainly, it's quite common. ... Sex, age, things like that. ... Do you own your house? Do you own a radio? I mean, the questions go quite beyond how many people there are," Roberts said.

"The CVAP, Citizen Voting Age Population, is the critical element in voting rights enforcement, and this is getting citizen information," he added.

The chief justice did not question whether Ross had concocted his justification for the citizenship question, as Furman had found. The challengers' lawyers avoided the topic as well, instead stressing that Commerce Department had violated standards and safeguards for policy changes under the Administrative Procedure Act.

The only person to use the word "contrived" and emphasize the possible pretextual grounds was Justice Elena Kagan. "You can't read this record without sensing that this need is a contrived one," she told US Solicitor General Noel Francisco, representing Ross and the Commerce Department.

Based on the tenor of the arguments, journalists and legal commentators presumed that the court was headed for a 5-4, conservative-liberal, vote. Inside the court after initial deliberations, that was how the justices divided.

But the decision-writing process is fluid. As they draft and share opinions, justices sometimes change positions, subtly in ways that affect parts of the legal reasoning undergirding an opinion, or more dramatically in ways that affect the entire outcome.

Sources familiar with deliberations say that two such noteworthy changes occurred last session, in the census controversy and in a dispute over the validity of a federal sex offender registration act.

In that latter case, the justices upheld the act by a 5-3 vote . (Justice Brett Kavanaugh did not participate in that case heard on the second day of the new session because he was not yet confirmed for the bench.)

Earlier in those negotiations that began in October and stretched until June, a different five-justice majority was headed in the opposite direction, toward invalidating the law and potentially limiting the authority of Congress to delegate power to another branch of government. (The 2006 Sex Offender Registration and Notification Act gave the attorney general authority to determine how the law applies to some convicts; Roberts, who dissented from the final decision preserving Congress' delegation power, did not switch his vote in this case, Gundy v. United States .)

The momentous announcement

On the last day of the annual term, Roberts showed his hand, but not without some suspense.

The first 24 pages of his 29-page census ruling rejected the challengers' arguments based on the Constitution's Enumeration Clause and the requirements of the Administrative Procedure Act. Roberts said judges should broadly defer to the administration as it devises the census form.

Provided the justification is truthful, that is.

To accept Ross' explanation grounded in the Voting Rights Act, the chief justice suggested in the final pages of his opinion, would essentially allow administration officials to dupe judges. Roberts said there was "a significant mismatch between the decision the Secretary made and the rationale he provided."

Declared Roberts, quoting the late US Appeals Court Judge Henry Friendly, for whom he once worked, "Our review is deferential, but we are 'not required to exhibit a naivete from which ordinary citizens are free.'"

The four other conservative justices, dissenting on this part of the ruling, believed Furman had wrongly probed Ross' rationale. Justice Clarence Thomas, joined by Justices Neil Gorsuch and Kavanaugh in an opinion, expressed suspicions of Furman's own motives as he had detailed evidence of pretext against Ross.

"I do not deny that a judge predisposed to distrust the Secretary or the administration could arrange those facts on a corkboard and -- with a jar of pins and a spool of string -- create an eye-catching conspiracy web." (Justice Samuel Alito separately dissented.)

Trump's immediate reaction was disbelief.

"Can anyone really believe that as a great Country, we are not able the ask whether or not someone is a Citizen," Trump tweeted on June 27. "Only in America!"

Roberts' decision, in theory, allowed Ross to try a new justification that would support asking people their citizenship status, and for two weeks after the ruling the administration considered ways to follow through on the 2020 plan.

But deadlines for printing the forms were at hand, and the motives of administration officials remained suspect. Trump announced on July 11 that he was dropping the plan to modify the 2020 long form and would seek citizenship information through other federal data collections.

.....United States Supreme Court is given additional information from which it can make a final and decisive decision on this very critical matter. Can anyone really believe that as a great Country, we are not able the ask whether or not someone is a Citizen. Only in America! — Donald J. Trump (@realDonaldTrump) June 27, 2019

Politics and new evidence

Roberts has strived to protect the reputation of the judiciary and to counteract what might be seen as predictable moves by him and the four other Republican-appointed justices who hold the majority. The four liberal justices are all Democratic appointees.

On the morning of June 27, both sides got a win.

Minutes before announcing that he voted against Republican interests in the census case, Roberts issued a separate, arguably more enduring, decision closing federal court doors to lawsuits against extreme partisan gerrymanders. Republicans currently dominate the country's statehouses, where district maps are drawn and have increasingly entrenched incumbents, and Democrats have increasingly tried to challenge those in court.

Roberts was joined in that case, centering on challenges to districts in North Carolina and Maryland, by his fellow justices on the right, as the four justices on the left passionately dissented. The political nature of the case and that divide between justices generated exactly the kind of criticism -- that justices are merely politicians in black robes -- that Roberts spent much of the 2018-2019 session trying to thwart.

"We do not serve one party or one interest," Roberts had said at the start of the last term he tried to defuse some of the tensions from Kavanaugh's confirmation hearings. "We serve one nation."

JUST WATCHED Trump fires back after Roberts rebukes him (2018) Replay More Videos ... MUST WATCH Trump fires back after Roberts rebukes him (2018) 02:57

There's also the question of the new evidence.

It is not known how Roberts might have been influenced in the census dispute by information that emerged on May 30 in news reports and court filings that appeared to reinforce the possibility that Ross had not been truthful.

One set of challengers, represented by the American Civil Liberties Union, notified the high court that day of new evidence purporting to show that a longtime Republican redistricting strategist, Thomas Hofeller, may have provided the Voting Rights Act enforcement rationale used by Ross.

Hofeller, who died last year in North Carolina, apparently wanted a citizenship count from the census to eventually provide an electoral advantage in redistricting endeavors for, in his words, "Republicans and Non-Hispanic Whites."

Francisco, in a June 3 court filing , dismissed the information as an "eleventh-hour campaign to improperly derail the Supreme Court's resolution of the government's appeal."

Consolation for the Trump administration

Roberts' census opinion didn't leave the Trump administration empty-handed -- something the Justice Department has been quick to recognize and use.

In the upcoming session, as the justices take up a consequential calendar of cases testing LGBTQ rights in the workplace, gun regulations and possibly abortion rights, they will also review new challenges to Trump policy.

Last month, the Justice Department used Roberts' own words from the census case as it defended Trump's effort to end the Obama-era policy of delaying deportation of young, undocumented immigrants brought to the US as children. The justices are scheduled to hear a dispute in November related to ending the Deferred Action for Childhood Arrivals, or DACA, program.

The scope of the court's review of the new Trump policy under the Administrative Procedure Act must be "narrow," DOJ argues, quoting Roberts, adding, again per Roberts: "A court may not substitute its judgment for that of the agency."

Finally, DOJ says judges should not "second-guess" a Cabinet secretary's choices that fall, in Roberts' words, "within the range of reasonable options."

What is ultimately found to be "reasonable" in the new 2019-2020 Supreme Court session remains in the hands of a chief justice who, long known as personally enigmatic, had become similarly inscrutable on the law.