Wales' leading doctor has announced he will be stepping down after eight and a half years in the role.