After listening to a nuclear weapons historian give a presentation at my school this question has become really intriguing to me.
I bet you listened to ME! :-D
So you have two great questions here, intertwined: the first is "why did South Africa build nuclear weapons?" and the second is "why didn't Spain or Turkey build them?" And we could include West Germany, the Netherlands, Italy, and lots of other countries in the latter categories.
To answer the second question first: one of the reason that the US pushed hard to create NATO, and to eventually "share" nuclear weapons with it (we still keep US nuclear weapons in the Netherlands, Turkey, and Belgium) was to keep these countries from feeling like they had to "go nuclear." In other words, the US didn't really want Europe to be filled with small nuclear powers — but they understood that these countries, many of which were ravaged by World War II, were not super keen on being possibly invaded by the USSR in some future war. They also acknowledged that if these countries didn't have some "skin in the game" (e.g., their own nukes of a sort), then the question might get asked: is the US willing to trade Brussels (or Paris, or West Berlin) for Washington or New York City? That is, these states worried that the US wasn't totally committed to defending them, and that could lead to the US allowing the Soviets to do things to them if there was a possibility the US could avoid its own destruction by doing so.
So the US tried to square this circle by putting US-controlled nukes inside many of these countries, under the idea that this would keep these countries from developing their own nuclear programs, and make it clear that the US "nuclear umbrella" was a protective one. It is of note that in the case of Turkey, Eisenhower agreeing to put nuclear missiles there dramatically increased tensions with the USSR and was one of the causes of the Cuban Missile Crisis. So it wasn't all roses.
That aside: what's up with South Africa? Why did they feel the need to make their own weapons? One thing to keep in mind is that South Africa (unlike, say, Turkey or Spain) is what we call a "pariah state." Their apartheid policies made it so that they didn't have very many strong allies in the world. So while the US did see them as a kinda-ally (in that they both opposed Communism), it wasn't buddy-buddy with them. The US didn't give South Africa any guarantees to its security.
The South African apartheid government worried about all sorts of security threats. They were an inherently "besieged" government: they were an overtly racist state in which a small white minority ruled over a large Black African majority, and were surrounded on all sides by other African nations, some of whom were attracted to the liberating promises of international Communism. As decolonization built up over the 1960s and 1970s South Africa became increasingly isolated on its own continent, and there were increasing pressures on it from within and without.
So how would an atomic bomb help that problem? Well, the answer is: it doesn't. I mean, it's not at all clear who they thought they'd use it on. A neighboring country? They didn't have an international "reach" (they could only drop them out of planes, and their planes had limited range), so they couldn't threaten the USSR or China or anyone at a long distance with them.
Historians who have studied the South African nuclear program have tended to emphasize that the traditional explanation of "what is the nuke supposed to do" (e.g. "give security") don't totally explain why they went down the nuclear path. They're part of the motivation. But in talking with former members of the South African apartheid government, and looking at the archival record, it looks more like South Africa sort of "slumped" into a bomb because there were tight connections between the military, a somewhat paranoid government, and the mining industry, the latter of which was used to provide the uranium for the weapon. In other words, the bomb "seemed like a good idea" but its purposes were never super well-defined. They only made about half a dozen of them, in the end — a tiny stockpile by global standards (even North Korea probably has more than that at this point). And this is part of why they found, as apartheid began to look like it was going to end in the early 1990s, that they could live without them: they never had that many of them, and they never really articulated what they were going to do, and they started to look like more of a liability than an asset. So it's a very different situation for a lot of reasons than the NATO members.