South Africa kept their hopes of retaining the Rugby Championship alive with a 30-22 victory over Australia in Cape Town.