This is discussed at length in the first chapter of Hilchos Avodah Zarah, particularly the beginning of Halacha 3:
בימי אנוש טעו בני האדם טעות גדול ונבערה עצת חכמי אותו הדור ואנוש עצמו
מן הטועים היה וזו היתה טעותם אמרו הואיל והאלהים ברא כוכבים אלו וגלגלים
להנהיג את העולם ונתנם במרום וחלק להם כבוד והם שמשים המשמשים לפניו
ראויין הם לשבחם ולפארם ולחלוק להם כבוד וזהו רצון האל ברוך הוא לגדל
ולכבד מי שגדלו וכבדו כמו שהמלך רוצה לכבד העומדים לפניו וזהו כבודו של
מלך כיון שעלה דבר זה על לבם התחילו לבנות לכוכבים היכלות ולהקריב להן
קרבנות ולשבחם ולפארם בדברים ולהשתחוות למולם כדי להשיג רצון הבורא בדעתם
הרעה וזה היה עיקר עבודת כוכבים וכך היו אומרים עובדיה היודעים עיקרה לא
שהן אומרים שאין שם אלוה אלא כוכב זה הוא שירמיהו אומר מי לא ייראך מלך
הגוים כי לך יאתה כי בכל חכמי הגוים ובכל מלכותם מאין כמוך ובאחת יבערו
ויכסלו מוסר הבלים עץ הוא כלומר הכל יודעים שאתה הוא לבדך אבל טעותם
וכסילותם שמדמים שזה ההבל רצונך הוא
ואחר שארכו הימים עמדו בבני האדם נביאי שקר ואמרו שהאל צוה ואמר להם עבדו
כוכב פלוני או כל הכוכבים והקריבו לו ונסכו לו כך וכך ובנו לו היכל ועשו
צורתו כדי להשתחוות לו כל העם הנשים והקטנים ושאר עמי הארץ ומודיע להם
צורה שבדה מלבו ואומר זו היא צורת הכוכב פלוני שהודיעוהו בנבואתו והתחילו
על דרך זו לעשות צורות בהיכלות ותחת האילנות ובראשי ההרים ועל הגבעות
ומתקבצין ומשתחוים להם ואומרים לכל העם שזו הצורה מטיבה ומריעה וראוי
לעובדה וליראה ממנה וכהניהם אומרים להם שבעבודה זו תרבו ותצליחו ועשו כך
כך ואל תעשו כך וכך והתחילו כוזבים אחרים לעמוד ולומר שהכוכב עצמו או
הגלגל או המלאך דבר עמהם ואמר להם עבדוני בכך וכך והודיע להם דרך עבודתו
ועשו כך ואל תעשו כך ופשט דבר זה בכל העולם לעבוד את הצורות בעבודות
משונות זו מזו ולהקריב להם ולהשתחוות וכיון שארכו הימים נשתכח השם הנכבד
והנורא מפי כל היקום ומדעתם ולא הכירוהו ונמצאו כל עם הארץ הנשים והקטנים
אינם יודעים אלא הצורה של עץ ושל אבן וההיכל של אבנים שנתחנכו מקטנותם
להשתחוות לה ולעבדה ולהשבע בשמה והחכמים שהיו בהם כגון כהניהם וכיוצא בהן
מדמין שאין שם אלוה אלא הכוכבים והגלגלים שנעשו הצורות האלו בגללם
ולדמותן אבל צור העולמים לא היה שום אדם שהיה מכירו ולא יודעו אלא יחידים
בעולם כגון חנוך ומתושלח נח שם ועבר ועל דרך זה היה העולם הולך ומתגלגל
עד שנולד עמודו של עולם והוא אברהם אבינו
כיון שנגמל איתן זה התחיל לשוטט בדעתו והוא קטן והתחיל לחשוב ביום ובלילה
והיה תמיה היאך אפשר שיהיה הגלגל הזה נוהג תמיד ולא יהיה לו מנהיג ומי
יסבב אותו כי אי אפשר שיסבב את עצמו ולא היה לו מלמד ולא מודיע דבר אלא
מושקע באור כשדים בין עובדי כוכבים הטפשים ואביו ואמו וכל העם עובדי
כוכבים והוא עובד עמהם ולבו משוטט ומבין עד שהשיג דרך האמת והבין קו הצדק
מתבונתו הנכונה וידע שיש שם אלוה אחד והוא מנהיג הגלגל והוא ברא הכל ואין
בכל הנמצא אלוה חוץ ממנו וידע שכל העולם טועים ודבר שגרם להם לטעות זה
שעובדים את הכוכבים ואת הצורות עד שאבד האמת מדעתם ובן ארבעים שנה הכיר
אברהם את בוראו כיון שהכיר וידע התחיל להשיב תשובות על בני אור כשדים
ולערוך דין עמהם ולומר שאין זו דרך האמת שאתם הולכים בה ושיבר הצלמים
והתחיל להודיע לעם שאין ראוי לעבוד אלא לאלוה העולם ולו ראוי להשתחוות
ולהקריב ולנסך כדי שיכירוהו כל הברואים הבאים וראוי לאבד ולשבר כל הצורות
כדי שלא יטעו בהן כל העם כמו אלו שהם מדמים שאין שם אלוה אלא אלו:
כיון שגבר עליהם בראיותיו בקש המלך להורגו ונעשה לו נס ויצא לחרן והתחיל
לעמוד ולקרוא בקול גדול לכל העולם ולהודיעם שיש שם אלוה אחד לכל העולם
ולו ראוי לעבוד והיה מהלך וקורא ומקבץ העם מעיר לעיר ומממלכה לממלכה עד
שהגיע לארץ כנען והוא קורא שנאמר ויקרא שם בשם ה' אל עולם וכיון שהיו העם
מתקבצין אליו ושואלין לו על דבריו היה מודיע לכל אחד ואחד כפי דעתו עד
שיחזירהו לדרך האמת עד שנתקבצו אליו אלפים ורבבות והם אנשי בית אברהם
ושתל בלבם העיקר הגדול הזה וחבר בו ספרים והודיעו ליצחק בנו וישב יצחק
מלמד ומזהיר ויצחק הודיע ליעקב ומינהו ללמד וישב מלמד ומחזיק כל הנלוים
אליו ויעקב אבינו למד בניו כולם והבדיל לוי ומינהו ראש והושיבו בישבה
ללמד דרך השם ולשמור מצות אברהם וצוה את בניו שלא יפסיקו מבני לוי ממונה
אחר ממונה כדי שלא תשכח הלמוד והיה הדבר הולך ומתגבר בבני יעקב ובנלוים
עליהם ונעשית בעולם אומה שהיא יודעת את ה' עד שארכו הימים לישראל במצרים
וחזרו ללמוד מעשיהן ולעבוד כוכבים כמותן חוץ משבט לוי שעמד במצות אבות
ומעולם לא עבד שבט לוי עבודת כוכבים וכמעט קט היה העיקר ששתל אברהם נעקר
וחוזרין בני יעקב לטעות העולם ותעיותן ומאהבת ה' אותנו ומשמרו את השבועה
לאברהם אבינו עשה משה רבינו רבן של כל הנביאים ושלחו כיון שנתנבא משה
רבינו ובחר ה' ישראל לנחלה הכתירן במצות והודיעם דרך עבודתו ומה יהיה
משפט עבודת כוכבים וכל הטועים אחריה
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 God 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 God, 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 God.
This was the essence of the worship of false gods, and this was the
rationale of those who worshiped them. They would not say that there
is no other god 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 God. Their foolish error consists of conceiving of this
emptiness 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.
He would inform them of a form that he had conceived, and tell them
that this is the image of the particular star, claiming that this was
revealed to him in a prophetic vision. In this manner, the people
began to make images in temples, under trees, and on the tops of
mountains and hills.
People would gather together and bow down to them and the [false
prophets] would say: This image is the source of benefit or harm. It
is appropriate to serve it and fear it. Their priests would tell them:
This service will enable you to multiply and be successful. Do this
and this, or do not do this or this.
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,
[God'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.
The wise men among them would think that there is no God other than
the stars and spheres for whose sake, and in resemblance of which,
they had made these images. The Eternal Rock was not recognized or
known by anyone in the world, with the exception of a [few]
individuals: for example, Chanoch, Metushelach, Noach, Shem, and Ever.
The world continued in this fashion until the pillar of the world -
the Patriarch 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] were idol worshipers, and he would
worship 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 God who controlled the sphere, that He created
everything, and that there is no other God among all the other
entities. He 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.
Abraham was forty years old when he became aware of his Creator. When
he recognized and knew Him, he began to formulate replies to the
inhabitants of Ur Kasdim and debate with them, telling them that they
were not following a proper path.
He broke their idols and began to teach the people that it is fitting
to serve only the God of the world. To Him [alone] is it fitting to
bow down, sacrifice, and offer libations, so that the people of future
[generations] would recognize Him. [Conversely,] it is fitting to
destroy and break all the images, lest all the people err concerning
them, like those people who thought that there are no other gods
besides these [images].
When he overcame them through the strength of his arguments, the king
desired to kill him. He was [saved through] a miracle and left for
Charan. [There,] he began to call in a loud voice to all people and
inform them that there is one God in the entire world and it is proper
to serve Him. He would go out and call to the people, gathering them
in city after city and country after country, until he came to the
land of Canaan - proclaiming [God's existence the entire time] - as
[Genesis 21:33] states: "And He called there in the name of the Lord,
the eternal God."
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 Abraham.
He planted in their hearts this great fundamental principle, composed
texts about it, and taught it to Isaac, his son. Isaac also taught
others and turned [their hearts to God]. He also taught Jacob and
appointed him as a teacher.
[Jacob] taught others and turned [the hearts] of all those who
gathered around him [to God]. 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 God and observe the
mitzvot of Abraham.
[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
Jacob and those who collected around them, until there became a nation
within the world which knew God.
When the Jews extended their stay in Egypt, however, they learned from
the [Egyptians'] deeds and began worshiping the stars 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 Abraham 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 God's love for us, and to uphold the oath He made to
Abraham, our patriarch, He brought forth Moses, our teacher, the
master of all prophets, and sent him [to redeem the Jews]. After
Moses, our teacher, prophesied, and God chose Israel as His
inheritance, He crowned them with mitzvot and informed them of the
path to serve Him, [teaching them] the judgement prescribed for idol
worshiper and all those who stray after it.