Deputy President Cyril Ramaphosa has won the leadership of South Africa's ruling African National Congress in a tightly fought contest.