With the scoreless match inching toward a penalty shootout, and the Netherlands down to 10 men, Andres Iniesta collected a pass from Cesc Fabregas to fire home from close range and break Dutch hearts four minutes from the final whistle.
It was the first time the European champions had reached a World Cup final and a bitter disappointment for the Netherlands who finished runners-up for the third time after previously losing the 1974 and 1978 finals.
It was also the first triumph by a European side outside their continent.
The result caused agony in the Netherlands and ecstasy in Madrid where fans shed tears of joy and fireworks went off all over the city. More than 100,000 people had gathered to watch on giant screens and many went hoarse with screams of joy.
In contrast, Dutch cities became deadly silent as their dream of finally winning the trophy evaporated. Some 180,000 fans trudged away quietly from Amsterdam's museum square.
The game was far from the classic final to Africa's first World Cup that many had hoped for from the two most impressive teams in the tournament.
Spain looked dominant for long passages but a ferociously physical Netherlands broke up their rhythm and their renowned precision passing in midfield, often reducing the game to a sterile stalemate.
BRUISING ENCOUNTER
In a bruising encounter in the huge Soccer City stadium, referee Howard Webb showed 13 yellow cards -- a record for the final -- and sent off Dutch defender John Heitinga in extra time.
Despite the disappointing quality of the match, it was a triumphant end to a highly successful tournament for South Africa who confounded years of pessimistic predictions that it would be ruined by violent crime and chaos.
To cap their pride, Nelson Mandela, much loved father of post-apartheid South Africa, briefly toured the pitch waving to the 85,000-strong crowd just before the match began.
Mandela, 91, who is in frail health, was driven around in a golf cart surrounded by bodyguards and greeted by an adoring crowd chanting his clan name Madiba.
The former president, who led South Africa out of apartheid in 1994, was unable to attend the opening ceremony because of the death of his great grand-daughter in a car accident the night before.
Spanish coach Vicente del Bosque told television: "It was a very difficult match. We have fantastic players. We could have scored another goal or two. But I think the result was deserved. It's a very happy day for me."
Dutch coach Bert van Marwijk said the better team had won in the end: "It is very sad. These three or four final minutes and then you lose. I really had thought even with 10 we would be able to do the penalty shoot out.
"It is bitter but that is sport. It is harsh."
Both sides missed good goalscoring opportunities, most notably Dutch winger Arjen Robben who was twice denied by brilliant saves from Spanish goalkeeper Iker Casillas.
TELEVISION AUDIENCE
FIFA said a worldwide television audience of more than 700 million had been expected to watch the final.
The victory will raise spirits in Spain which is torn by bitter political infighting over how to address Europe's highest unemployment and crippling debt.
Despite being the first host nation knocked out at the group stage, South Africans have remained enthusiastic spectators, helping this tournament to be the third best attended, with three million seats sold, behind the United States in 1994 and Germany in 2006. President Jacob Zuma dubbed them true champions.
Chief local organizer Danny Jordaan said the tournament had brought the races together and enabled South Africans to walk tall after being told for years they were inferior.
But African joy at a tournament credited with rebranding both South Africa and the continent was blighted by two bomb blasts in the Ugandan capital Kampala that killed at least 23 people watching the final in bars, police said. There was no immediate claim of responsibility.
(Reporting by Reuters World Cup team and Stella Mapenzauswa; Editing by Ossian Shine)