The national assembly passed the bill 230-41 with three abstentions on Tuesday.
It now has to go to the National Council of Provinces, which is expected to be a formality, before being signed into law by Thabo Mbeki, the president.
Veterans of the governing African National Congress praised the Civil Union Bill as an example of the spirit of the country's first post-apartheid constitution, adopted a decade ago.
Nosiviwe Mapisa-Nqakula, the home affairs minister, said: "When we attained our democracy, we sought to distinguish ourselves from an unjust painful past, by declaring that never again shall it be that any South African will be discriminated against on the basis of colour, creed, culture and sex."
The bill provides for the "voluntary union of two persons, which is formalised and registered by either a marriage or civil union".
It does not specify whether they are heterosexual or homosexual partnerships.
It was drafted to comply with a court ruling last December that said existing marriage legislation was unconstitutional as it discriminated against same-sex couples.
South Africa's constitution was the first in the world to prohibit discrimination on the basis of sexual orientation, but many in the country are still conservative on such issues.
Kenneth Meshoe, a politician, described the passing of the bill as the "saddest day in our 12 years of democracy".
Homosexuality is illegal in Zimbabwe, Kenya, Uganda, Nigeria, Tanzania, Ghana and many other sub-Saharan countries.
Given the ANC's huge majority, the government can push through almost any legislation it wants. But it had to order politicians to respect the party line and push supporters of the anti-apartheid movement to convince reluctant traditionalists.
Mosuia Lekota, the defence minister, reminded politicians that many homosexuals went into exile and prison with ANC members during apartheid.
He said: "The roots of this bill lie in many years of struggle.
"The country cannot afford to be a prison of timeworn prejudices which have no basis in modern society.
Let us bequeath to future generations a society which is more democratic and tolerant than the one that was handed down to us."
The Roman Catholic church and many leaders objected to the use of "marriage" saying this denigrated the sanctity of traditional marriages.
In an effort to ease some of these concerns, the bill allowed religious and civil officers to refuse to marry same-sex couples on moral grounds.
Gay rights groups criticised this opt-out clause, but in general praised the new measure.
Fikile Vilakazi, who works for a network of 17 gay and lesbian organisations, said: "It demonstrates powerfully the commitment of our lawmakers to ensuring that all human beings are treated with dignity."