England have kept their Test with South Africa alive after some crucial wickets late into day two after a batting collapse.