Very curious about this. As far as I know, they weren't a major player in the Cold War and they weren't under any immediate threat, so I do wonder why they undertook all of the effort to make a nuclear program.
South Africa developed nuclear weapons for much the same reason that North Korea did. As an international pariah, it could not count on any outside support for its defense.
During the 1960s and afterward, South Africa's white minority government faced increased pressure from black South Africans seeking an end to apartheid and black guerilla movements backed by the Soviet Union. South Africa's defense expenditures exploded, but at the same time the rest of the world was abandoning a white supremacist view of the world. In the United States, the Civil Rights movement of the 1960s and subsequent liberalization made it politically unfeasible for America to support a literally racist South African government.
The 2005 book South Africa's Weapons of Mass Destruction contends that Prime Minister John Vorster's decision to develop nuclear weapons was a perversion of the American Plowshares initiative Atoms for Peace program, which promoted peaceful uses of atomic energy. The United States supplied highly enriched uranium to build South Africa's Safari-1 research reactor, as well as reams of data about atomic energy, including nuclear weapons.
In the 1960s, the South African government explored the possibility of using atomic detonations for mining, but military uses were never far below the surface. In 1970, for example, Vorster announced South Africa's uranium enrichment program and said its goal was atomic power, but he warned that South Africa would not be limited to peaceful uses of atomic energy.
In 1974, Vorster OK'd the development of nuclear weapons, which proceeded apace for the next several years. In 1975, anti-apartheid sanctions stopped the flow of highly enriched uranium to South Africa, but South Africa had its own enrichment plant up and running at full speed by 1978. The Koeberg nuclear power station, whose construction began in 1976, was the cover for all this.
The timing of the development is an auspicious one. In 1974, a military coup toppled the government in Portugal, and the nation's new leaders signed an agreement granting independence to Angola, a nation just north of Namibia, which at the time was occupied by South Africa and considered a South African province. The situation in Angola rapidly deteriorated into a multi-sided civil war, with Soviet-backed, Chinese-backed, and American-backed factions fighting each other. South Africa intervened as well, and Cuban advisers soon deployed into the area, fighting South Africans on behalf of Soviet-backed forces.
The United States, weary of foreign intervention due to the Vietnam War, withdrew from the situation, and domestic pressure ensured that it could not support South Africa. That nation would have to deal with the situation in Angola (and in Namibia, Botswana, and other places) on its own.
In 1977, South Africa was preparing to test a mockup of a bomb (it lacked nuclear material), but an American (or other Western) spy plane overflew the site and canceled the test. It is hypothesized that an unconfirmed nuclear detonation in 1979 in the southern Indian Ocean was a covert test of a South African device.
Until the South African government decided to dismantle its atomic arsenal in 1989, it built six atomic bombs and was at work on a seventh. The existence of these weapons was not revealed until 1993.
At the time, President F. W. de Klerk (who dismantled the program when he became prime minister in 1989) said the seven bombs were the minimum necessary for a "credible deterrent capability." Their development was made "against the background of a Soviet expansionist threat in southern Africa and South Africa's relative international isolation and the fact that it could not rely on outside assistance, should it be attacked."
Those quotes are from the New York Times story linked above, which also explains South Africa's strategy:
"'The strategy was that, if the situation in southern Africa were to deteriorate seriously, a confidential indication of the deterrent capability would be given to one or more of the major powers, for example, the United States, in an attempt to persuade them to intervene,' he said.
Another official, who spoke on condition of anonymity, said, 'The thinking was, if we were attacked from the north, we would have taken one of the devices to the Kalahari and tested it -- then turned to the United States and said, "We need you to send the Marines." ' "