The Supreme Court in a landmark decision on Friday ruled gay and lesbian couples have a constitutionally protected right to marry and that states must recognize those marriages, handing advocates a sweeping and historic victory that cements the legality of same-sex marriage nationwide.

Thirty-seven states and the District of Columbia permitted same-sex marriage ahead of Friday's 5-4 ruling in Obergefell v. Hodges, a case in which the justices considered questions related to gay marriage restrictions in four states. Justice Anthony Kennedy authored the majority's opinion and was joined by the court's liberal wing. The court's conservative justices each filed a dissent.

In answering whether the Constitution grants same-sex couples the right to marry, Kennedy – considered the court's key swing vote – based his decision on the recognition of four fundamental principles, arguing that marriage is inherent to the concept of individual autonomy, that it is of unparalleled importance to committed couples, that it is crucial for safeguarding the rights of the children of couples in committed relationships and that it has long been a keystone of social order.

He said "no union is more profound than marriage" and that the due process clause of the 14th Amendment extends to "certain personal choices central to individual dignity and autonomy, including intimate choices that define personal identity and beliefs."

"The nature of injustice is that we may not always see it in our own times," Kennedy wrote in the 34-page opinion. "The generations that wrote and ratified the Bill of Rights and the Fourteenth Amendment did not presume to know the extent of freedom in all of its dimensions, and so they entrusted to future generations a charter protecting the right of all persons to enjoy liberty as we learn its meaning."

He also said the same amendment's equal protection clause "prohibits this unjustified infringement of the fundamental right to marry," noting the court's 1967 ruling in Loving v. Virginia – which invalidated prohibitions on interracial marriage – contained a similar finding.

"It is now clear that the challenged laws burden the liberty of same-sex couples, and it must be further acknowledged that they abridge central precepts of equality," Kennedy wrote. "Here the marriage laws enforced by the respondents are in essence unequal: same-sex couples are denied all the benefits afforded to opposite-sex couples and are barred from exercising a fundamental right."

Kennedy additionally addressed the possibility of preserving the system by which marriages recognized in one state are denied in another, saying that "[l]eaving the current state of affairs in place would maintain and promote instability and uncertainty."

In a dissenting opinion, Chief Justice John Roberts argued that the issue was not within the court's realm to decide.

"This Court is not a legislature," Roberts wrote, calling the majority's decision "an act of will, not legal judgment." "Whether same-sex marriage is a good idea should be of no concern to us. Under the Constitution, judges have power to say what the law is, not what it should be."

Roberts also said he would "begrudge none their celebration" following the ruling, but said the majority's approach was "deeply disheartening." All the dissenting judges indicated the decision amounted to overreach on the part of the judiciary, with suggestions that the ruling interrupted the democratic process through which the legality of gay marriage had been steadily extending across the U.S.

"Stealing this issue from the people will for many cast a cloud over same-sex marriage, making a dramatic social change that much more difficult to accept," the chief justice said.

The announcement of the decision prompted a celebration from a crowd of hundreds of people assembled outside the court. Supporters cheered, waved the rainbow flag that has become a symbol for lesbian, gay, bisexual and transgender rights and spontaneously broke into a rendition of the national anthem.

During remarks at the White House, President Barack Obama noted that progress often comes in slow increments, but then “there are days like this,” in which “slow steady effort is rewarded with justice that arrives like a thunderbolt.”

He went on to call the court’s ruling a “victory for America.”

“This decision affirms what millions of Americans already believe in their hearts: When all Americans are treated as equal, we are all more free,” he said.