Concerning the above, Rabbi Mosheh ben-Maimon also known as the Rambam (1138–1204 CE) wrote in the Mishnah Torah - Hilchoth Avodah Zara and Laws of the Nations Chapter 1:
During the times of Enosh, mankind made a great mistake, and the wise men of that generation gave thoughtless counsel. Enosh himself was one of those who erred.
Their mistake was as follows: They said the Elohim (singular) created stars and spheres with which to control the world. He placed them on high and treated them with honor, making them servants who minister before Him. Accordingly, it is fitting to praise and glorify them and to treat them with honor. [They perceived] this to be the will of The Source of reality (האל), blessed be He, that they magnify and honor those whom He magnified and honored, just as a king desires that the servants who stand before him be honored. Indeed, doing so is an expression of honor to the king.
After conceiving of this notion, they began to construct temples to the stars and offer sacrifices to them. They would praise and glorify them with words, and prostrate themselves before them, because by doing so, they would - according to their false conception - be fulfilling the will of The Creator (הבורא). This was the foundation of Avodah Zara.
Thus, those who did this and knew the foundation; they would not say that there is no other power / source of reality except for this star.
This message was conveyed by Jeremiah, who declared (10:7-8): "Who will not fear You, King of the nations, for to You it is fitting. Among all the wise men of the nations and in all their kingdoms, there is none like You. They have one foolish and senseless [notion. They conceive of their] empty teachings as wood;" i.e., all know that You alone are The Source (האל). Their foolish error consists of conceiving the empty / false concepts they created as Your will.
After many years passed, there arose people - false prophets - who told [their nations] that God had commanded them to say: Serve this star - or all the stars - sacrifice to it, offer libations to it, build a temple for it and make an image of it so that all people - including the women, the children, and the common people - could bow to it.........
Subsequently, other deceivers arose and declared that a specific star, sphere, or angel had spoken to them and commanded them: Serve me in this manner. He would then relate a mode of service [telling them:] Do this, do not do this.
Thus, these practices spread throughout the world. People would serve images with strange practices - one more distorted than the other - offer sacrifices to them, and bow down to them. As the years passed, [Hashem's] glorious and awesome name was forgotten by the entire population. [It was no longer part of] their speech or thought, and they no longer knew Him. Thus, all the common people, the women, and the children would know only the image of wood or stone and the temples of stone to which they were trained from their childhood to bow down and serve, and in whose name they swore.
(Above from Chabad.com translation of the Hebrew with linquistic edits by me)
Rabbi Yohhai Maqbili in his version of the Mishnah Torah composes this as the Rambam stating that there were three phases to how Avodah Zara started in human history.
Skipping ahead the Mishnah Torah - Hilchoth Avodah Zara and Laws of the Nations Chapter 1 states the following.
The world continued in this fashion until the pillar of the world - the Patriarch Avraham (Abraham) - was born.
After this mighty man was weaned, he began to explore and think. Though he was a child, he began to think [incessantly] throughout the day and night, wondering: How is it possible for the sphere to continue to revolve without having anyone controlling it? Who is causing it to revolve? Surely, it does not cause itself to revolve.
He had no teacher, nor was there anyone to inform him. Rather, he was mired in Ur Kasdim among the foolish idolaters. His father, mother, and all the people [around him] did Avodah Zara [were idol worshipers], and he would worship [Avodah Zara] with them. [However,] his heart was exploring and [gaining] understanding.
Ultimately, he appreciated the way of truth and understood the path of righteousness through his accurate comprehension. He realized that there was one Eloah / Power / Source / etc. (אלוה אחד) who controlls reality, that He created everything, and that there is no other Eloah / Power / Source / etc. (אלוה אחד) except for Him. He [Avraham] knew that the entire world was making a mistake. What caused them to err was their service of the stars and images, which made them lose awareness of the truth. Avraham was forty years old when he became aware of his Creator.
When the people would gather around him and ask him about his statements, he would explain [them] to each one of them according to their understanding, until they turned to the path of truth. Ultimately, thousands and myriads gathered around him. These are the men of the house of Avraham.
He planted in their hearts this great fundamental principle, composed texts about it, and taught it to Yitzhhaq (Isaac), his son. Yitzhhaq (Isaac) also taught others and turned [their hearts to Hashem]. He also taught Ya'aqov (Jacob) and appointed him as a teacher.
Ya'aqov (Jacob) taught others and turned [the hearts] of all those who gathered around him [to Hashem]. He also taught all of his children. He selected Levi and appointed him as the leader. He established him [as the head of] the academy to teach them the way of Hashem and observe the mitzvot of Avraham.
Ya'aqov (Jacob) commanded his sons that the leadership should not depart from the descendants of Levi, so that the teachings would not be forgotten. This concept proceeded and gathered strength among the descendants of Ya'aqov (Jacob) and those who collected around them, until there became a nation within the world which knew Hashem.
When the Jews extended their stay in Egypt, however, they learned from the [Egyptians'] deeds and began worshiping the stars [Avodah Zara) as they did, with the exception of the tribe of Levi, who clung to the mitzvot of the patriarchs - the tribe of Levi never served false gods.
Within a short time, the fundamental principle that Avraham had planted would have been uprooted, and the descendants of Jacob would have returned to the errors of the world and their crookedness. Because of Hashem's love for us, and to uphold the oath He made to Avraham, our patriarch, He brought forth Mosheh ben-Amram (Moses), our teacher, the master of all prophets, and sent him. After Mosheh ben-Amram (Moses), our teacher, prophesied, and Hashem chose Yisrael as His inheritance, He crowned them with mitzvot and informed them of the path to serve Him, [teaching them] the judgement prescribed for Avodah Zara and all those who stray after it.
(Above from Chabad.com translation of the Hebrew with linquistic edits by me)