Ben Stokes led the line as South Africa lost five wickets in the final session on a dramatic last day in Cape Town