Match ends, South Africa 1, Ghana 2.
Andre Ayew scored a late goal to put Ghana into the Africa Cup of Nations quarter-finals as Group C winners.
Ayew bravely headed home Baba Rahman's left-wing cross after his side had earlier looked like being eliminated.
Mandla Masango had given South Africa the lead with possibly the goal of the tournament when he scored a spectacular volley from 25 yards.
Ghana piled on the pressure after the break and substitute John Boye drove home an equaliser before Ayew struck.
With 17 minutes remaining, Ghana were on the way home as they trailed 1-0 but Boye's equaliser gave them hope and then Ayew's strike confirmed they would finish ahead of Algeria, who qualified with them after beating Senegal 2-0, on head-to-head record.
Ghana had never scored against South Africa in an Africa Cup of Nations match but they started as though they were determined to end that record.
Jonathan Mensah headed just wide in the fifth minute and 60 seconds later, Jordan Ayew's effort was cleared off the line by Eric Mathoho.
Masango gave South Africa a 17th-minute lead in stunning fashion when a defensive header fell to him outside the penalty area and he produced an unstoppable shot.
Ghana chased the equaliser and Asamoah Gyan's shot was punched clear by Nhlanhla Khuzwayo.
South Africa still had their moments and Bongani Ndulula's powerful header was well parried away by Brimah Razak just before half-time.
The Black Stars dominated the second half with Gyan curling a free-kick just over and then having a cross punched off his head by Khuzwayo.
The goal they threatened came in the 73rd minute when Boye, a first-half substitute for the injured Daniel Amartey, pounced on Andre Ayew's scuffed shot, took a touch and drove it underneath Khuzwayo.
With Algeria 1-0 up, Ghana needed another goal to avoid going out to Senegal on their head-to-head record.
Cambridge United's Kwesi Appiah then had his shot turned on to the post by Khuzwayo and the Black Stars poured forward - before they took the lead in the 83rd minute.
Rahman delivered a teasing cross from the left and Ayew met it with a firm header from eight yards out.
That was enough to earn Ghana a quarter-final against the runners-up from Group D on Sunday.
The result meant South Africa had taken the lead in all three of their Group C matches but failed to win any of them.
- 6NgcongcaBooked at 37mins
- 18PhalaSubstituted forVilakaziat 58'minutes
- 15FurmanBooked at 79mins
- 23RantieSubstituted forLetsholonyaneat 73'minutes
- 9NdululaSubstituted forParkerat 85'minutes
- 1BrimahBooked at 90mins
- 18AmarteySubstituted forBoyeat 36'minutes
- 6AcquahSubstituted forBaduat 70'minutes
- 9J AyewSubstituted forAppiahat 70'minutes
- 10A Ayew
- Hamada Nampiandraza
- Shots on Target
Second Half ends, South Africa 1, Ghana 2.
Foul by Asamoah Gyan (Ghana).
Rivaldo Coetzee (South Africa) wins a free kick in the defensive half.
Mubarak Wakaso (Ghana) wins a free kick on the left wing.
Foul by Mandla Masango (South Africa).
Razak Brimah (Ghana) is shown the yellow card.
André Ayew (Ghana) wins a free kick in the defensive half.
Foul by Reneilwe Letsholonyane (South Africa).
Attempt blocked. Reneilwe Letsholonyane (South Africa) right footed shot from outside the box is blocked. Assisted by Bernard Parker.
Foul by Mubarak Wakaso (Ghana).
Mandla Masango (South Africa) wins a free kick in the attacking half.
Christian Atsu (Ghana) wins a free kick in the defensive half.
Foul by Dean Furman (South Africa).
Attempt blocked. Dean Furman (South Africa) right footed shot from outside the box is blocked.
Substitution, South Africa. Bernard Parker replaces Bongani Ndulula.
Corner, South Africa. Conceded by John Boye.
Attempt blocked. Bongani Ndulula (South Africa) right footed shot from long range on the right is blocked. Assisted by Andile Jali.
Goal! South Africa 1, Ghana 2. André Ayew (Ghana) header from the centre of the box to the high centre of the goal. Assisted by Baba Rahman with a cross.
Attempt blocked. Christian Atsu (Ghana) left footed shot from the centre of the box is blocked. Assisted by Mubarak Wakaso.