WASHINGTON — It has long been established that public workers as a general matter may not be fired or demoted for speaking out on political topics. The Supreme Court on Tuesday ruled on a case that involved a twist on that question: What if the official doing the demoting misunderstood what the worker had done?

The justices, in a 6-to-2 decision, said it was unconstitutional to demote a police officer based on the mistaken assumption that he had engaged in political activity.

“When an employer demotes an employee out of a desire to prevent the employee from engaging in political activity that the First Amendment protects, the employee is entitled to challenge that unlawful action,” Justice Stephen G. Breyer wrote for the majority, “even if, as here, the employer makes a factual mistake about the employee’s behavior.”

The case, Heffernan v. Paterson, N.J., No. 14-1280, concerned Jeffrey J. Heffernan, a police detective in Paterson. He was seen with a sign for a mayoral candidate, Lawrence Spagnola, leaving the impression that he supported the candidate. In fact, Mr. Heffernan’s bedridden mother had asked him to pick up the sign for her. Mr. Heffernan had taken no position on the candidate.

He was nonetheless demoted to patrol officer based on a supervisor’s understanding that he had been making a political statement.

“In this way, they punished Heffernan for what they thought was his ‘overt involvement’ in Spagnola’s campaign,” Justice Breyer wrote of city officials. “In fact, Heffernan was not involved in the campaign but had picked up the sign simply to help his mother. Heffernan’s supervisors had made a factual mistake.”

Mr. Heffernan sued and lost, with the United States Court of Appeals for the Third Circuit, in Philadelphia, ruling that his First Amendment rights had not been violated because he had not exercised them.

Justice Breyer disagreed, saying it was the government’s motive rather than the worker’s activities that mattered. “The government acted upon a constitutionally harmful policy whether Heffernan did or did not in fact engage in political activity,” he wrote.

“The upshot is that a discharge or demotion based upon an employer’s belief that the employee has engaged in protected activity can cause the same kind, and degree, of constitutional harm whether that belief does or does not rest upon a factual mistake,” Justice Breyer wrote.

Chief Justice John G. Roberts Jr. and Justices Anthony M. Kennedy, Ruth Bader Ginsburg, Sonia Sotomayor and Elena Kagan joined the majority opinion, which returned the case to the lower courts to explore whether the city might have demoted Mr. Heffernan on other, lawful grounds.

In dissent, Justice Clarence Thomas said the majority had focused on the wrong factor. The worker’s actions and not the government’s motives mattered, he said.

“Demoting a dutiful son who aids his elderly, bedridden mother may be callous, but it is not unconstitutional,” Justice Thomas wrote in an opinion joined by Justice Samuel A. Alito Jr.

“The majority surmises that an attempted violation of an employee’s First Amendment rights can be just as harmful as a successful deprivation of First Amendment rights,” Justice Thomas wrote.

“But harm alone is not enough; it has to be the right kind of harm,” he added.