Washington (CNN)In a landmark opinion, a divided Supreme Court on Friday ruled that same-sex couples can marry nationwide, establishing a new civil right and handing gay rights advocates a historic victory.

In the 5-4 ruling, Justice Anthony Kennedy wrote for the majority with the four liberal justices. Each of the four conservative justices wrote their own dissent.

Nearly 46 years to the day after a riot at New York's Stonewall Inn ushered in the modern gay rights movement, the decision could settle one of the major civil rights fights of this era. The language of Kennedy's opinion spoke eloquently of the most fundamental values of family, love and liberty.

READ: The Supreme Court's full decision

"No union is more profound than marriage, for it embodies the highest ideals of love, fidelity, devotion, sacrifice and family," Kennedy wrote. "In forming a marital union, two people become something greater than they once were."

'Equal dignity in the eyes of the law'

"Their hope," Kennedy wrote, "is not to be condemned to live in loneliness, excluded from one of civilization's oldest institutions. They ask for equal dignity in the eyes of the law. The Constitution grants them that right."

Kennedy has written the opinion in significant gay rights cases and when he uttered the key sentence that same-sex couples should be able to exercise the right to marry in all states, people in the Court's public gallery broke into smiles and some wiped tears from their eyes.

Reaction: People soak up history from coast to coast

In a dissent, Justice Antonin Scalia blasted the Court's "threat to American democracy."

"The substance of today's decree is not of immense personal importance to me," he wrote. "But what really astounds is the hubris reflected in today's judicial Putsch."

Chief Justice John Roberts wrote that the decision had "nothing to do with the Constitution."

READ: The best lines from Scalia's marriage dissent and Kennedy's decision

"If you are among the many Americans—of whatever sexual orientation—who favor expanding same-sex marriage, by all means celebrate today's decision. Celebrate the achievement of a desired goal," he wrote. "Celebrate the opportunity for a new expression of commitment to a partner. Celebrate the availability of new benefits. But do not celebrate the Constitution. It had nothing to do with it."

The U.S. is now the 21st country to legalize same-sex marriage nationwide, including territories. Married same-sex couples will now enjoy the same legal rights and benefits as married heterosexual couples nationwide and will be recognized on official documents such as birth and death certificates.

Hundreds of same-sex marriage supporters flooded the plaza and sidewalk in front of the Court to celebrate the ruling, proudly waving rainbow flags and banners with the Human Rights Campaign's equal sign, which have come to represent the gay rights movement. In an emotional moment, the supporters sang the National Anthem, clapping wildly after singing that the U.S. is "the land of the free."

WATCH: Obama calls gay marriage case plaintiff Jim Obergefell

After the ruling, President Barack Obama called Jim Obergefell, the lead plaintiff in the case, while he and his supporters celebrated the ruling outside the court.

Obama: 'Congratulations'

"I just wanted to say congratulations," Obama said as CNN broadcast his warm words to Obergefell over speakerphone. He added: "Your leadership on this has changed the country."