(Genesis 49:28:) "ALL THESE ARE THE TRIBES OF ISRAEL, TWELVE IN NUMBER." These are the tribes; but concerning Ishmael it is written (Genesis 17:20): "HE SHALL BEGET TWELVE PRINCES." As for the tribes, the matter follows the order of the world: the day is twelve hours, the night is twelve hours, the year is twelve months, the constellations are twelve. Therefore, "ALL THESE ARE THE TRIBES OF ISRAEL, TWELVE." Rabbi Yohanan said: But were there twelve tribes? Surely Ephraim and Manasseh make fourteen! Rather, when Levi was counted among them, Ephraim and Manasseh were not counted among them; and when Levi was not counted among them, Ephraim and Manasseh were counted among them as one tribe. From where? As it is said (Numbers 1:10): "OF THE CHILDREN OF JOSEPH: OF EPHRAIM, ELISHAMA SON OF AMMIHUD." Thus, "ALL THESE ARE THE TRIBES OF ISRAEL, TWELVE" — no fewer and no more. Even Rebekah saw them [as twelve], as it is said (Genesis 25:23): "AND THE LORD SAID TO HER: TWO NATIONS ARE IN YOUR WOMB" — here are two; "AND TWO PEOPLES" — here are four; "ONE PEOPLE SHALL BE MIGHTIER THAN THE OTHER PEOPLE" — here are six; "AND THE ELDER SHALL SERVE THE YOUNGER" — here are eight; "AND BEHOLD, THERE WERE TWINS IN HER WOMB" — here are ten; "AND THE CHILDREN STRUGGLED WITHIN HER" — here are twelve.
Why Israel Has Exactly Twelve Tribes in Tanchuma Vayechi
Midrash Tanchuma Buber, Vayechi 16:1
כל אלה שבטי ישראל שנים עשר. אלו השבטים, אבל לבלן בישמעאל שנים עשר נשיאים יוליד (בראשית יז כ), אבל השבטים על סדרו של עולם הוא מהלך, היום י"ב שעות, הלילה י"ב שעות, השנה י"ב חדשים, המזלות י"ב, לפיכך כל אלה שבטי ישראל שנים עשר. אמר ר' יוחנן, וכי שנים עשר שבטים היו, והלא אפרים ומנשה הרי י"ד, אלא כשלוי נמנה עמן אין אפרים ומנשה נמנה עמהן, וכשאין לוי נמנה עמהן אפרים ומנשה נמנין עמהן שבט אחד, מנין שנאמר לבני יוסף לאפרים אלישמע בן עמיהוד (במדבר א י), הוי כל אלה שבטי ישראל שנים עשר לא פחות ולא יותר. אפילו רבקה ראתה אותם [שנים עשר] שנאמר ויאמר ה' לה שני גוים בבטנך (בראשית כה כג), הרי ב', ושני לאומים הרי ד', ולאום מלאום יאמץ הרי ו', ורב יעבוד צעיר הרי ח', והנה תאומים בבטנה הרי י', ויתרוצצו הבנים בקרבה הרי י"ב.