WASHINGTON — The Supreme Court on Thursday said admissions officials at the University of Texas may consider the race of student applicants in a limited way to build a diverse student body.

The 4-to-3 decision was a surprising win for advocates of affirmative action, who say the benefits of diversity at the nation’s colleges and universities are worth the intrusion on the Constitution’s guarantee of equal protection that generally forbids the government from making decisions based on racial classifications.

Justice Anthony Kennedy said the university’s consideration of race was a “factor of a factor of a factor” and met the court’s narrow precedents.

“A university is in large part defined by those intangible qualities which are incapable of objective measurement but which make for greatness,” Kennedy wrote in the court’s majority opinion.

“Considerable deference is owed to a university in defining those intangible characteristics, like student body diversity, that are central to its identity and educational mission,” he wrote. “But still, it remains an enduring challenge to our Nation’s education system to reconcile the pursuit of diversity with the constitutional promise of equal treatment and dignity.”

When the court considered the case in 2013, Kennedy wrote the opinion that sent it back to lower courts for a closer examination, and it seemed that the majority was skeptical that the admissions plan would survive.

Justice Samuel Alito began his 51-page dissent: “Something strange has happened since our prior decision in this case.” And he added that “the university has still not identified with any degree of specificity the interests that its use of race and ethnicity is supposed to serve.”

He read the lengthy dissent from the bench to stress his disagreement. Chief Justice John Roberts and Justice Clarence Thomas joined his opinion.

Kennedy had never before voted to uphold a race-conscious plan, but he also had been reluctant to say that race may never be used. He was joined by three of the court’s liberal justices: Ruth Bader Ginsburg, Stephen Breyer and Sonia Sotomayor. Justice Elena Kagan recused herself because she had worked on the issue as President Obama’s solicitor general.

The specific case was brought in 2008 by Abigail Fisher, a white woman who was denied admission to the university. Her suit was organized and funded by a conservative legal organization that opposes racial preferences in government and brought the challenge that resulted in the justices striking a key provision of the Voting Rights Act in 2013.

“I am disappointed that the Supreme Court has ruled that students applying to the Univ. of Texas can be treated differently because of their race or ethnicity. I hope that the nation will one day move beyond affirmative action,” Fisher said in a statement.