South Africa are on the brink of elimination at the ICC World Cup after Kane Williamson guided New Zealand to a four-wicket victory with three balls to spare.