Mandla Masango gave South Africa the lead in the 17th minute, when he smashed home past Ghana goalkeeper Razak Braimah from outside the box. The Black Stars drew level in the 73rd minute through defender John Boye, whose drilled shot screamed under a diving goalkeeper Nhlanhla Khuzwayo. Ayew then grabbed the winner in the 83rd minute through a diving header off a cross by Abdul Rahman Baba.
That left Ghana as Group C winners on six points with Algeria finishing second, also on six points after they beat Senegal 2-0 in the other game being played simultaneously in Malabo. This was Ghana's first win at the Nations Cup against Bafana Bafana after two defeats and a draw.
After South Africa went in front against the run of play, Ghana poured forward in search of an equalising goal. A thunderbolt by Abdul Rahman Baba from outside the South Africa area in the 33rd minute was pushed away by goalkeeper Khuzwayo Six minutes later, South Africa striker Bongani Ndulula forced goalkeeper Braimah to dive low and keep out his goal-bound header at the near post.
In the 48th minute, skipper Gyan's free-kick from a good position narrowly missed the mark as Ghana continued to attack in the hope of getting back into the game. Seven minutes later it was the turn of Wakaso to force the goalkeeper to fumble his low shot drilled from distance for a corner. Celtic ace Wakaso again tried his luck from distance soon after, but again found no joy.
Ghana continued to pile on the pressure before they were rewarded in the 73rd minute through Boye and moments later, substitute Kwesi Appiah would have given the Black Stars the lead but his shot was turned away for a corner by the goalkeeper.
Their persistence eventually earned them the much-needed winner seven minutes from time and safe passage to the knockout rounds.
Algeria advance at Senegal's expense
Algeria secured their spot in the quarter-finals of the Africa Cup of Nations thanks to a 2-0 win against Senegal in their last Group C game in Malabo.
On a dramatic evening, Riyad Mahrez set Algeria on their way with the opening goal inside 11 minutes, and for a long time it looked as if both sides would be heading through to the last eight. Tottenham Hotspur midfielder Nabil Bentaleb then produced a superb finish to double Algeria's lead eight minutes from the end to seal a berth in the knockout stage for Christian Gourcuff's men.
Africa's best side in the FIFA/Coca-Cola World Rankings can now look forward to a quarter-final tie in Malabo on Sunday, while Senegal will have to wait at least another two years to end their search for a first Cup of Nations crown.
Algeria created very little in losing 1-0 to Ghana in their last outing, but the Desert Warriors should have been in front inside two minutes on this occasion, with Sofiane Feghouli passing up a glorious chance when left with just the goalkeeper to beat.
However, they did not have to wait long to get the breakthrough. The game was only 11 minutes old when a quickly-taken Madjid Bougherra free-kick on halfway completely caught out the sleeping five-man Senegal defence, allowing Mahrez to control and tuck home under Bouna Coundoul.
It could have been even worse for the Lions of Teranga with the tricky Yacine Brahimi opening up play for Hilal Soudani on the right, but the striker, making his first appearance at the finals, saw his powerful shot beaten away by the goalkeeper.
Senegal became more of a threat themselves as the half wore on with Southampton's Sadio Mane firing over and Papy Djilobodji sending a long-range free-kick narrowly wide. Coach Alain Giresse was then forced into a change as Pape Souare replaced starting left-back Cheikh Mbengue, who suffered a head wound in a clash with Aissa Mandi. Still they had opportunities to restore parity before the interval, though, as Rais Mbolhi failed to hold a speculative Idrissa Gueye strike and then denied Stephane Badji following a quickly-taken free-kick.
The score in the night's other game meant both Algeria and Senegal were heading through at the interval, but Giresse's side were eager to keep their fate in their own hands. The introduction of Newcastle United's Papiss Cisse early in the second half in place of Papa Diop saw Senegal play with three up front and they pinned Algeria back in search of an equaliser.
But it was Gourcuff's side who almost netted again with 15 minutes remaining, Kara Mbodj clearing a Saphir Taider shot off the line after Coundoul fumbled a Mahrez cross. And they did increase their lead with eight minutes left, the accomplished Bentaleb beating Coundoul with a thumping strike from the edge of the box after receiving a pass from Feghouli.
At that point both sides were still heading through, but a second goal for Ghana moments later in Mongomo ensured that Senegal would be heading home.