WASHINGTON (Reuters) - The Supreme Court ruled on Friday that the U.S. Constitution provides same-sex couples the right to marry, handing a historic triumph to the American gay rights movement.

The court ruled 5-4 that the Constitution’s guarantees of due process and equal protection under the law mean that states cannot ban same-sex marriages. With the landmark ruling, gay marriage becomes legal in all 50 states.

Immediately after the decision, same-sex couples in many of the states where gay marriage had been banned headed to county clerks’ offices for marriage licenses as officials in several states said they would respect the ruling.

President Barack Obama, appearing in the White House Rose Garden, hailed the ruling as a milestone in American justice that arrived “like a thunderbolt.”

“This ruling is a victory for America,” said Obama, the first sitting president to support gay marriage. “This decision affirms what millions of Americans already believe in their hearts. When all Americans are treated as equal, we are all more free.”

The ruling, the culmination of a long legal fight by gay rights advocates, follows steady gains in public approval in recent years for same-sex marriage. It was not until 2004 that the Massachusetts became the first state to legalize gay marriage. But the decision may provoke fresh legal fights in some conservative, Republican-governed states.

Justice Anthony Kennedy, writing on behalf of the court, said the hope of gay people intending to marry “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.”

“Without the recognition, stability and predictability marriage offers, their children suffer the stigma of knowing their families are somehow lesser,” Kennedy wrote.

Kennedy, a conservative who often casts the deciding vote in close cases, was joined in the majority by the court’s four liberal justices.

Appointed by Republican President Ronald Reagan in 1988, Kennedy has now authored all four of the court’s major gay rights rulings, with the first in 1996.

The ruling is the Supreme Court’s most important expansion of marriage rights in the United States since its landmark 1967 ruling in the case Loving v. Virginia that struck down state laws barring interracial marriages.

At least two states, Louisiana and Mississippi, said they would not immediately issue marriage licenses to same-sex couples while awaiting legal formalities. Supreme Court rulings generally take 25 days to go into effect.

‘THREAT TO AMERICAN DEMOCRACY’

In a blistering dissenting opinion, conservative Justice Antonin Scalia said the decision shows the court is a “threat to American democracy.” The ruling “says that my ruler and the ruler of 320 million Americans coast-to-coast is a majority of the nine lawyers on the Supreme Court,” Scalia added.

Conservative Chief Justice John Roberts read a summary of his dissent from the bench, the first time he has done so in his 10 years on the court. Roberts said although there are strong policy arguments in same-sex marriage, it was not the court’s role to force states to change their marriage laws.

“Five lawyers have closed the debate and enacted their own vision of marriage as a matter of constitutional law,” Roberts wrote.

The dissenters raised concerns about the impact of the case on people opposed to same-sex marriage on religious grounds.

Slideshow ( 15 images )

While the ruling only affects state laws and religious institutions can still choose whether to marry same-sex couples, Roberts predicted future legal conflicts.

“Hard questions arise when people of faith exercise religion in ways that may be seen to conflict with the new right to same-sex marriage,” Roberts said.

There were 13 state bans in place, while another state, Alabama, had contested a court ruling that lifted the ban there.

The ruling is the latest milestone in the gay rights movement. In 2010, Obama signed a law allowing gays to serve openly in the U.S. military. In 2013, the high court ruled unconstitutional a 1996 U.S. law that declared for the purposes of federal benefits marriage was defined as between one man and one woman.

Slideshow ( 15 images )

Reaction came swiftly. James Obergefell, the Ohio man who was the lead plaintiff in the case, told a cheering crowd outside the Supreme Court, “Today’s ruling from the Supreme Court affirms what millions across this country already know to be true in our hearts - our love is equal, that the four words etched onto the front of the Supreme Court - equal justice under law - apply to us, too.”

Obama telephoned Obergefell to offer congratulations.

Hundreds of gay rights supporters celebrated outside the courthouse with whoops and cries of “U-S-A!” and “Love is love” as the decision came down.

In Ann Arbor, Michigan, another of the plaintiffs, April DeBeor, told cheering supporters, “To my beautiful children, we did this for you,” referring to her adopted children.

“Love won,” added popular comedian and talk show host Ellen DeGeneres, who is gay.

‘JUDICIAL TYRANNY’

Conservatives denounced the ruling. Republican presidential candidate Mike Huckabee called it “an out-of-control act of unconstitutional judicial tyranny.”

Wisconsin Republican Governor Scott Walker, also running for president, called for amending the U.S. Constitution to allow states to again ban same-sex marriage. Texas Republican Governor Greg Abbott said, “Marriage was defined by God. No man can redefine it.”

Archbishop Joseph Kurtz of Louisville, Kentucky, president of the U.S. Conference of Catholic Bishops, said, “It is profoundly immoral and unjust for the government to declare that two people of the same sex can constitute a marriage.”

Opponents say same-sex marriage’s legality should be decided by states, not judges. Some opponents argue it is an affront to traditional marriage between a man and a woman and that the Bible condemns homosexuality.

Gay marriage also is gaining acceptance in other Western countries. Last month in Ireland, voters backed same-sex marriage by a landslide in a referendum that marked a dramatic social shift in the traditionally Roman Catholic country.

Ireland followed several Western European countries including Britain, France and Spain in allowing gay marriage, which is also legal in South Africa, Brazil and Canada. But homosexuality remains taboo and often illegal in many parts of Africa and Asia.

The Supreme Court’s ruling came in a consolidated case pulling together challenges filed by same-sex couples to gay marriage bans in Kentucky, Michigan, Ohio and Tennessee.

The Obama administration argued on the side of the same-sex marriage advocates.