Mexico had looked the sharper and more inventive side in the first half, and could have taken an earlylead when Itumeleng Khune spilled Paul Aguilar'scross on virtually their firstattack.
Midfielder Xavi Alonso netted the winner in the 107th minute when his curling free-kick evaded his own players, the South African defenders and unsighted goalkeeper Itumeleng Khune.
South Africa now face a final group game against France without suspended keeper Khune, midfielder Dikgachoi, who picked up a second yellow of the tournament, and with only slim hopes of qualifying.