(CNN) -- A stunning free-kick from Barcelona full-back Daniel Alves with just two minutes remaining proved enough to give Brazil a narrow 1-0 victory over host nation South Africa in Thursday's second Confederations Cup semifinal in Johannesburg.
Daniel Alves is congratulated by team-mate Maicon after scoring the winner against South Africa.
With the match heading for extra-time, substitute Alves smashed home a vicious strike from the edge of the area, after a foul on Ramirez by Aaron Mokeona, as Dunga's side secured a clash with the United States -- whom they have already beaten in the group stages -- in Sunday's Ellis Park final.
The United States secured their place in the tournament's climax after ending Spain's unbeaten record in stunning style on Wednesday.
Meanwhile, South Africa have the consolation of facing European champions Spain once again in the third-place play-off in Rustenburg.
Despite their victory, Brazil rarely convinced in a tight encounter, as South Africa's Brazilian coach Joel Santana sent his side out intent to frustrate the country of his birth.
South Africa goalkeeper Itumelang Khune safely held onto Andre Santos' volley before new Real Madrid signing Kaka darted through the South African defence, but his curving shot drifted wide of the far post.
Mokoena then missed a gilt-edge chance, putting his close-range header wide from Teko Modise's superbly-floated free-kick.
Brazil were fortunate not to go into the break trailing as Everton winger Steven Pienaar had goalkeeper Cesar beaten, but his stinging shot edged just wide.
The second half was also tight, with defences dominating but the home side nearly went ahead when Modise's close-range shot deflected off Luisao before hitting the beaten Cesar's right-hand post.
Robinho and Luis Fabiano both fired wide for Brazil, who seemed unlikely to break the deadlock. However, with eight minutes remaining, Dunga introduced Alves into the fray and the Barcelona defender repaid his confidence when he beat the Bafana Bafana wall with a thundering free-kick.