(CNN) -- The California Supreme Court struck down the state's ban on same-sex marriage Thursday, saying sexual orientation, like race or gender, "does not constitute a legitimate basis upon which to deny or withhold legal rights."
Gay couple John Lewis, left, and Stuart Gaffney celebrate outside the California Supreme Court on Thursday.
In a 4-3 120-page ruling issue, the justices wrote that "responsibly to care for and raise children does not depend upon the individual's sexual orientation."
"We therefore conclude that in view of the substance and significance of the fundamental constitutional right to form a family relationship, the California Constitution properly must be interpreted to guarantee this basic civil right to all Californians, whether gay or heterosexual, and to same-sex couples as well as to opposite-sex couples," Chief Justice Ronald George wrote for the majority.
The ruling takes effect in 30 days. Watch what the ruling means »
Several gay and lesbian couples, along with the city of San Francisco and gay rights groups, filed a lawsuit saying they were victims of unlawful discrimination. A lower court ruled San Francisco acted unlawfully in issuing marriage licenses to same-sex couples.
The ruling surprised legal experts because the court has a reputation for being conservative. Six of its seven judges are Republican appointees.
San Francisco City Attorney Dennis Herrera said he is "profoundly grateful" for the decision and for the court's "eloquence" in its delivery.
"After four long years, we're very, very gratified," he said.
Shannon Minter, attorney for one of the plaintiffs in the case, the National Center for Lesbian Rights, called the ruling "a moment of pure happiness and joy for so many families in California."
"California sets the tone, and this will have a huge effect across the nation to bringing wider acceptance for gay and lesbian couples," he said.
Neil Giuliano, president of the Gay and Lesbian Alliance Against Defamation, issued a statement saying, "Today's ruling affirms that committed couples, gay and straight, should not be denied the duties, obligations and protections of marriage. ... This decision is a vital affirmation to countless California couples -- straight and gay -- who want to make and have made a lifelong commitment to take care of and be responsible for each other."
Groups opposing same-sex marriage also reacted strongly to the ruling.
"The California Supreme Court has engaged in the worst kind of judicial activism today, abandoning its role as an objective interpreter of the law and instead legislating from the bench," said Matt Barber, policy director for cultural issues for the group Concerned Women for America, in a written statement.
"So-called 'same-sex' marriage is counterfeit marriage. Marriage is, and has always been, between a man and a woman. We know that it's in the best interest of children to be raised with a mother and a father. To use children as guinea pigs in radical San Francisco-style social experimentation is deplorable."
The organization said that a constitutional marriage amendment should be placed on the November ballot and that national efforts should be made to generate a federal marriage amendment.
"The decision must be removed from the hands of judicial activists and returned to the rightful hands of the people," Barber said.
A constitutional amendment initiative specifying that marriage is only between a man and a woman is awaiting verification by the secretary of state's office after its sponsors said they had gathered enough signatures to place it on the statewide ballot. The parties cannot appeal to the U.S. Supreme Court, Herrera said, as federal courts do not have jurisdiction over the state laws. "This is the final say," he said.
In a dissenting opinion, Associate Justice Marvin Baxter wrote that although he agrees with some of the majority's conclusions, the court was overstepping its bounds in striking down the ban. Instead, he wrote, the issue should be left to the voters.
In 2004, San Francisco officials allowed gay couples in the city to wed, prompting a flood of applicants crowding the city hall clerk's office. The first couple to wed then was 80-year-old Phyllis Lyon and 83-year-old Dorothy Martin, lovers for 50 years.
"We have a right just like anyone else to get married to the person we want to get married to," Lyon said at the time.
San Francisco Mayor Gavin Newsom called the ruling a victory not just for the city "but for literally millions of people. ... What the court did is simply affirm their lives."
CNN's Ted Rowlands reported that "huge cheers" went up in San Francisco when the ruling was announced.
In California, a 2000 voter referendum banned same-sex marriage, but state lawmakers have made two efforts to allow gay and lesbian couples to wed. Republican Gov. Arnold Schwarzenegger vetoed both bills.
"I respect the court's decision and as governor, I will uphold its ruling," Schwarzenegger said in a statement issued Thursday. "Also, as I have said in the past, I will not support an amendment to the constitution that would overturn this state Supreme Court ruling."
Massachusetts legalized same-sex marriages in 2004, and gay couples need not be state residents there to wed. However, then-Gov. Mitt Romney resurrected a 1913 law barring non-resident marriages in the state if the marriage would be prohibited in the partners' home state.
Subsequent court and agency decisions have determined that only residents of Massachusetts, Rhode Island or New Mexico may marry in the state, unless the marriage partners say they intend to relocate to Massachusetts after the marriage.
New Hampshire, Vermont, New Jersey and Connecticut permit civil unions, and California has a domestic-partner registration law. More than a dozen other states give gay couples some legal rights, as do some other countries. Check the law in different states »
"It's a throwaway line, but I think it's true: As California goes, so goes the rest of the nation," Newsom said. "And I don't think people should be paranoid about that. ... Look what happened in Massachusetts a number of years ago. Massachusetts is doing just fine. The state is doing wonderfully."
The state law in question in the case, which consolidated six cases, was the Defense of Marriage Act, Proposition 22. Oral arguments in March lasted more than three hours.
"There can be no doubt that extending the designation of marriage to same-sex couples, rather than denying it to all couples, is the equal protection remedy that is most consistent with our state's general legislative policy and preference," the ruling said.
"Accordingly, in light of the conclusions we reach concerning the constitutional questions brought to us for resolution, we determine that the language of Section 300 limiting the designation of marriage to a 'union between a man and a woman' is unconstitutional, and that the remaining statutory language must be understood as making the designation of marriage available to both opposite-sex and same-sex couples."
Newsom compared the ruling to the 1967 U.S. Supreme Court ruling in a Virginia case overturning that state's ban on interracial marriage.
"This is about civil marriage. This is about fundamental rights," he said.
The ruling may make the same-sex marriage issue more important in November elections.
Presumptive GOP presidential nominee Sen. John McCain supports "traditional" marriage but opposes a constitutional amendment banning same-sex marriage, saying individual states should decide the issue. He also backs some legal benefits for same-sex couples.
Democratic presidential candidates Sen. Barack Obama and Sen. Hillary Clinton both oppose same-sex marriage but support civil unions. They also oppose a constitutional ban.
CNN's Bill Mears contributed to this report.
All About Same-Sex Marriage • San Francisco