Pro-marijuana activists cheered in the public gallery and chanted "Weed are free now" when the Constitutional Court gave its landmark ruling.
In a unanimous ruling, judges also legalised the growing of marijuana for private consumption.
South Africa's government's had opposed its legalisation, arguing the drug was "harmful" to people's the health.
Three cannabis users who had faced prosecution for using cannabis brought the case, saying the ban "intrudes unjustifiably into their private spheres".
be in possession of cannabis in private for his or her personal consumption."
It will, however, remain illegal to use cannabis in public, and to sell and supply it.
The Cannabis Development Council of South Africa welcomed the ruling, and called on the government to drop charges against people found in possession of the drug.
Jeremy Acton, the leader of the Dagga Party, which campaigns for the use of cannabis, said the ruling should have gone further to legalise the carrying of marijuana in public.