They chose them, for the most part, and there was no set way that names were chosen. Some adopted the name of their former owners. Some also chose last names based on their occupation, prominent local or national figures, based on the names other family members had already chosen, or based on a town or place.
I can't imagine they kept the last names they had in Africa before becoming slaves and shipped to the U.S., or torn apart from their families and could even remember their last names. and certainly the number of African-Americans with 'white' last names is conspicuous given this history.