The Constitutional Court gave Parliament a year to make the necessary legal changes, disappointing some activists, some of whom have been waiting years to marry.
"We were thinking we would be calling our friends today and inviting them to our wedding," said Fikile Vilakazi, of the Forum for the Empowerment of Women, who proposed to her partner more than six months ago. "Now they are asking us to wait another year."
South Africa recognized the rights of gay people in the constitution adopted after apartheid ended in 1994, the first in the world to prohibit discrimination on the basis of sexual orientation. But government has opposed attempts to extend the definition of marriage in court to include same-sex couples in the mostly Christian country.
Married couples have numerous rights still denied gay couples, including the ability to make decisions on each other's behalf in medical emergencies, and inheritance rights if a partner dies without a will.
Judge Albie Sachs, who delivered Thursday's ruling, said the common law and Marriage Act definitions of marriage as a union between a man and a woman "are accordingly inconsistent with sections ... of the Constitution to the extent that they make no provision for same-sex couples to enjoy the status, entitlements and responsibilities they accord to heterosexual couples."
The court instructed Parliament to extend the definitions within a year, or else the words "or spouse" would automatically be added by the courts, The South African Press Association reported.
Judge Kate O'Regan agreed with the other 10 members of the court that same-sex marriage should be legal, but argued in a separate opinion that the court should effect the changes immediately, a view shared by gay rights groups.
Vilakazi worried lawmakers could attempt to water down the decision by introducing a different category of marriage for same-sex couples. Proposals previously mooted in Parliament include the introduction of "civil unions," which would provide the same legal benefits as marriage but would not oblige religious institutions to solemnize them, she told The Associated Press.
Thursday's ruling was in response to an appeal by government against a Supreme Court ruling last year that said a lesbian couple's union should be recognized. There was no immediate comment on the decision from government.
Marie Fourie and Cecelia Bonthuys, a couple from Pretoria, brought the original application after the government refused to recognize their October 2002 wedding on the basis of the common law definition of marriage. They were not present in court Thursday.