The Supreme Court on Wednesday broadened the ability of religious organizations to hire and fire employees without offending federal anti-discrimination laws, affirming that the Constitution provides freedom to choose who teaches the faith.

In a 7-to-2 decision, the court strengthened the “ministerial exception” it found in 2012 that protects religious organizations from some employment lawsuits. It said two teachers at parochial schools who wanted to contest their firings in court were the kinds of employees who were covered by the exception and thus unable to sue.

It indicated that those involved in almost any kind of religious instruction would be considered “ministers” of the faith, no matter their official title or even if they practiced the faith themselves.

Justices Sonia Sotomayor and Ruth Bader Ginsburg dissented, saying the decision gave religious employers “free rein to discriminate.”

Judges on the U.S. Court of Appeals for the 9th Circuit in San Francisco had said the teachers could proceed with their lawsuits against Catholic schools in California.

Agnes Morrissey-Berru alleged age discrimination against Our Lady of Guadalupe School in the Los Angeles area. In the other case, Kristen Biel sued St. James School in Torrance. She said her firing after a diagnosis of breast cancer violated the Americans With Disabilities Act. (Biel lost her battle with the disease last year, and her husband, Darryl Biel, had taken her place in the lawsuit.)

But both “performed vital religious duties,” said Justice Samuel A. Alito Jr., who wrote for the court’s majority.

“The religious education and formation of students is the very reason for the existence of most private religious schools, and therefore the selection and supervision of the teachers upon whom the schools rely to do this work lie at the core of their mission,” Alito wrote.

“Judicial review of the way in which religious schools discharge those responsibilities would undermine the independence of religious institutions in a way that the First Amendment does not tolerate.”

Conservatives on the Supreme Court have been more broadly supportive of religious rights and organizations. Last week, the court ruled that states that aid private schools may not exclude religious ones.

But in the case decided Wednesday, Justices Stephen G. Breyer and Elena Kagan, usually part of the court’s liberal bloc, voted with the majority.

The teacher cases asked the court for further guidance on when an employee of a religious organization should be considered secular and, thus, able to take advantage of anti-discrimination laws — or religious, and thus unable to do so.

In a unanimous decision in 2012, the court said the “ministerial exception” meant that a former teacher at a Michigan Lutheran school could not sue her employer.

The key, Alito said, is the Constitution’s protection of the free exercise of religion.

“This does not mean that religious institutions enjoy a general immunity from secular laws, but it does protect their autonomy with respect to internal management decisions that are essential to the institution’s central mission,” he wrote. “And a component of this autonomy is the selection of the individuals who play certain key roles.”

As in 2012’s Hosanna-Tabor Evangelical Lutheran Church and School v. EEOC, the majority declined to set up a specific formula that should be applied in all cases.

Although they joined the majority opinion, Justices Clarence Thomas and Neil M. Gorsuch said that it did not go far enough and that any decisions about which employees qualified should be left up to the organizations’ “good-faith claims.”

“This deference is necessary because, as the court rightly observes, judges lack the requisite ‘understanding and appreciation of the role played by every person who performs a particular role in every religious tradition,’ ” they said.

Sotomayor said the court’s decision distorts its “careful” 2014 ruling and “strips thousands of schoolteachers of their legal protections.”

She continued: “It gives an employer free rein to discriminate because of race, sex, pregnancy, age, disability, or other traits protected by law when selecting or firing their ‘ministers,’ even when the discrimination is wholly unrelated to the employer’s religious beliefs or practices. That is, an employer need not cite or possess a religious reason at all; the ministerial exception even condones animus.”

Stanford law professor Jeffrey L. Fisher, who argued the case on behalf of the women, said he believed the decision would remove protection for about half of the 300,000 lay teachers in religious schools — “those who teach a general curriculum in elementary schools and those who teach religion in middle or high schools. It remains to be seen whether the court will declare employment discrimination laws unconstitutional as applied to the other half who teach only secular subjects.”

Eric Rassbach, a senior counsel at the Becket Fund for Religious Liberty, which represented the schools, said the decision was a “huge win for religious schools of all faith traditions.”

“The last thing government officials should do is decide who is authorized to teach Catholicism to Catholics or Judaism to Jews. We are glad the Court has resoundingly reaffirmed that churches and synagogues, not government, control who teaches kids about God,” he said in a statement.

The cases are Our Lady of Guadalupe School v. Morrissey-Berru and St. James School v. Biel.