The most direct answer to how or why this myth originated lies in the Old Testament and the belief that Ham, one of Noah's sons, was "cursed" (in fact, it is more specifically the son of Ham, and therefore Noah's grandson, upon whom this curse is placed) and told that his descendants would be "servants of servants" in perpetuity.
From reading the text in Genesis 9, it's unclear why this was ever thought to refer to black people, but this is the way it was interpreted, especially by white racists in antebellum America in order to justify slavery and claim that it was biblically ordained. One sometimes sees old maps concerning Old Testament genealogy in which the words "Descendants of Ham settled here" are written on sub-Saharan African. But it has always been a mystery to me how anyone would think to know either where the "descendants of Ham" settled or what their skin color would presumably be. It appears as though this view was retrospectively imposed upon the biblical text when theories of "divisions" among mankind became prevalent as long-distance contact and trade became more and more frequent between different peoples.
Probably because of the fact that for most of history before the 1400s CE, Europe had no contact with the portion of the African continent south of the Sahara, Europeans regarded themselves as being completely disconnected or distinct from the populations there. North Africa, on the other hand, had both a mythological and long-standing historical connection with Europe. The fact that North Africans are generally darker in skin color than Europeans thus did not cause them to be considered a distinctive "race" when this concept began to be tied up with color and when the notion arose of large, distinct segments of the the human population distinguished by color.
Once the West African slave trade had begun, it was convenient for those involved in that trade to identify the sub-saharan peoples as a separate, allegedly inferior "race" known as "Negroes" in order to justify enslaving them. Given the spectrum of human skin color that Europeans were (or should have been) already aware of as existing across peoples from northern to southern Europe, from northern to central Africa, throughout the Middle East, and on the Asian subcontinent from north to south, one would think they would have seen racial or color differences as a continuum rather than a separation into hard and fast "races." But obviously they did not, for reasons of economic power and myth-based notions of supremacy.
Perhaps the clearest proof that the entire concept of race is rooted in myth can be found in the European view of Ethiopia. At times throughout history and even recently, assertions were made that Ethiopians are not "Negroes," or are not "black." One probable reason for this view is that Ethiopians (or Abyssinians, as they were once called) adopted Christianity (and adopted it as their state religion) as early as the fourth century CE and had long had contact with the Mediterranean world. Therefore, despite skin color, which is supposed to be the defining factor of "race," Ethiopians were exempted from the myth-based definition of being "Negro." Racial theorists were ostensibly able to rationalize the "Caucasianizing" of North Africans because of the relative lightness of their skin color. But given the much darker coloring of people from Ethiopia, it's easy to see that the manner in which they have been categorized by Europeans invalidates the observable or physical basis of "race" as a concept.
We can safely conclude, I believe, that the general idea of race is based on myth when defined as anything other than an unbroken continuum of skin color.