New Zealands Kane Williamson delivered a masterclass in batting, guiding his team to victory over South Africa in the second match of the Tri-Nation series.