The custom of learning Torah through the night of Shavuot is a young one, by Jewish standards. Before the 1500s, it's possible that it was never practiced at all, or perhaps only by a few. Although there are some ideas that prefigure the custom in the Midrash and early commentators on the Torah, the idea of Tikkun Layl Shavuot really has its main root in the Zohar.
Rabbi Hiya compares the people of Israel leaving Egypt and counting seven weeks while traveling to Mt. Sinai to a noble woman counting seven days before her wedding. The night of Shavuot is the culmination of that process, the culmination of the counting, the night when the bride visits the Mikvah in advance of her wedding. We'll pick up the narrative there.
I find that this passage only opened up to me when I sat with it for a while, read it a few times, slowly, pictured it in my mind. A fine thing to do on the evening of Shavuot.
The earliest recorded case of a Tikkun Layl Shavuot being practiced was in Greece, in the year 1533 (or thereabouts), and it was a remarkable one. Present were R. Yosef Karo, later the author of the Shulchan Aruch, R. Shlomo HaLevi Alkabetz, later the composer of Lecha Dodi, and other unnamed members of their circle. A few years later, R. Alkabetz wrote of the experience. (Below is an excerpt from his letter. There's more to this story than we can tell here...)
And yet, when R. Yosef Karo wrote the Shulchan Aruch (~1560), he made no mention of staying up on the night of Shavuot. (See Shulchan Arukh, Orach Chayim 494:1). But other sources from the late 1500s show that the custom was alive in the golden age of Tzfat.
ערב שבועות: ישנים שעה אחת או ב' שעות, אחר שעשו צרכי מועד, לפי שבלילה אחר האכילה מתקבצים בבתי כנסיות, כל קהל וקהל בב"ה שלו, ואינם ישנים כל הלילה, וקורין תורה נביאים וכתובים, ומשניות וזהר ודרשות בקראי עד אור הבוקר, ואז כל העם טובלים בבוקר קודם תפלת שחרית, כדאיתא בזוהר פ' אמור.
Avraham Galante (d. 1589)
The evening before Shavuot: they sleep an hour or two, after preparing the needs of the holiday, because on the night after the meal they gather in the synagogues, each community in its synagogue, and don't sleep the whole night. And they read Torah, Prophets, and Writings, Mishnah, Zohar, and Drashot on verses until the light of morning. Then the people immerse in the mikvah in the morning before Shacharit, as is written in the Zohar parshat Emor.
In the next few centuries, the custom took hold and spread, until it became quite common. By the mid 1600s, the custom was already widespread.
The historian Elliott Horowitz presents a convincing case that the spread of coffee was a key factor in the adoption of the custom of Tikkun Layl Shavuot (and Tikkun Hatzot as well.)
(See "Coffee, Coffeehouses, and the Nocturnal Rituals of Early Modern Jewry")
R. Avraham Gombiner, the Magen Avraham, wrote in the mid 1600s. He seems uncomfortable with the mystical roots of the practice, and looks for a simpler reason.
The Midrash that the Magen Avraham draws from does have a critical element, though it could be read as offering a balanced view of sleep as well.
Perhaps this midrash led to the following comment from Avraham Ibn Ezra (1089 - 1164 CE).
I wonder if either of these were on the mind of the author of the Zohar.
Eliyahu Kitov offers support for those of us who need a few winks (or can't find coffee).
ועוד אמרו. שלפיכך נתקן סדר הלילה כדי לתקן פגם של מקבלי התורה הראשונים שהיו בהם רבים שהלכו לישון באותו הלילה והקב״ה עוררם מן השינה שיבואו לקבל התורה כדאיתא במדרש. ואין זו קטגוריא ח״ו על ישראל שבאותו הדור שכלם בני דעה היו ומצפים לשמוע דבר ה׳׳ אלא מפני שהיו בהם חלשים והתיראו שמא לא יוכלו לעמוד על כל כחס בשעת הדיבור אם לא יינפשו בלילה. ואנשי אמת היו כלם ולא עשו מפני הבושה מזה על זה ואם שיער אדם בנפשו ששנתו בלילה יפה לו כדי שישמע הדיבור והוא בכל כחו וזיוו, עשה שלא נאסרו בכך.
Book of Our Heritage
It is said that the custom of staying awake was enacted to repair the wrong of the generation that received the Torah - that many of them slept that night, and God had to rouse them. This is not a criticism of that generation, who were all conscious people who looked forward to hearing the word of God. Rather, they were tired, and were worried that they wouldn't be able to stand in their strength when they heard God speaking if they didn't sleep. They were all deeply honest people, and were not influenced by how others would perceive them. If a person felt that sleep would be good for him, in order to be at full strength to hear the Word, he did so, since there was no prohibition of sleeping.
There's room in the sources for different approaches to the night of Shavuot. Stay up! or Sleep. Orient yourself to the heavenly wedding of the Torah, to self improvement, or just to learn and enjoy.