Captain Sam Cane and hooker Samisoni Taukei'aho also scored tries as the All Blacks silenced the 61,519 crowd to claim what will be viewed as a famous win having lost five of their previous six tests.
The Springboks were not as clinical as they had been in the 26-10 victory over the visitors the previous week as they scored tries through sublime centre Lukhanyo Am and winger Makazole Mapimpi, but faced opponents who were vastly improved.
Whether the victory is enough to save Foster's job will become clear in the coming days, as New Zealand prepare to host Argentina in their next Rugby Championship clash on 27 August, while the Boks travel to play Australia on the same day.
The All Blacks were better in the scrum, breakdown and under the high ball, all areas they had struggled in seven days ago in Nelspruit.
"Proud is an understatement," Cane said at the post-match presentation. "Adversity really challenges your character and this group has that. We had to get a few parts of our game right as this is one of the toughest places in the world to come and play.
"We were a lot better at the breakdown and dealt with the contestables better. We defended the maul well. That is what test footy is all about, getting the small things right to build pressure."
It took until the 25th minute for New Zealand to open the score via a penalty, but the visitors then accelerated into a 15-0 lead.
They kept possession from a Caleb Clarke break and Cane crossed in the corner, before Taukei'aho barged over from close-range after incessant pressure.
The Boks had a good close to the half, though, as Am showed great strength to beat the tackle of Clarke and score, and first five eighth Handre Pollard landed a penalty from 55 yards that sailed through the Highveld air to make it 15-10 at the break.
The teams traded penalties before the Boks scored their second try, a trademark steel from Malcolm Marx at the breakdown saw Damian Willemse float a long pass for Mapimpi to cross in the corner.
The Boks took the lead for the first time on 68 minutes after Beauden Barrett tackled half back Jaden Hendrikse without the ball in his own 22 and received a yellow card.
But despite being a man down, the All Blacks produced a big finish as Havili and Barrett dotted down to complete the win.
"The first half, the game was fast and we couldn't put out game-plan on them," South Africa captain Siya Kolisi said.
"We could have worked harder there. We knew they only need a couple of moments to make it count and they did that. Congratulations to them."