One might naturally expect that the Jewish people would enter the land and then receive the Torah there. It seems logical to me that the Torah should come from a holy place characterized by the revelation of Hashem, in keeping with passages like "Ki mitziyon tetze Torah." The wilderness, in contrast, seems to represent a sort of exile, or at least an imperfect condition. So why was the Torah given in the desert rather than in Eretz Yisrael?
Mechilta Drav Yishmael - Yisro - Parsha 5 says that it was not given in Eretz Yisroel in order that the non Jews would not to be able to say that they did not accept it since it was given in the Jewish land. Another reason was to avoid a dispute between the Shevatim.
ומפני מה לא ניתנה תורה בארץ ישראל? שלא ליתן פתחון פה לאומות העולם, לומר: לפי שנתנה תורה בארצו, לפיכך לא קבלנו עלינו. דבר אחר: שלא להטיל מחלוקת בין השבטים, שלא יהא זה אומר: בארצי נתנה תורה, וזה אומר: בארצי נתנה תורה, [אלא] במדבר ובאש ובמים. לומר לך: מה אלו חנם לכל באי העולם, כך דברי תורה חנם לכל באי העולם .
Additionally, it was given in the desert (no-man's land) so that no people would be able to claim that they have no share in the Torah. (See English comments in the Stone Chumash; I can't give a more specific reference because I don't have the book on my lap ATM, sorry).
edit: Mekhilta De-Rabbi Ishmael (Exodus 19:2).
Spinoza in his Theological-Political Treatise argued that the legislation of Judaism was political legislation, necessary for the conduct of a state. According to Spinoza, the end of Jewish sovereignty made the law of Judaism irrelevant. The Torah was given before the people entered the land because Spinoza is wrong--the Jewish people are bound by the Torah even in exile.
Michael Wyschogrod in his Body of Faith expands on this idea as follows:
[T]he national identities of other nations are land-bound identities. The nation is defined by the territory it occupies. But [the Jewish nation] comes into national existence before it occupies the land. It becomes a nation on the basis of a promise delivered to it when it is a stranger in the land of others. This awareness of being a stranger is burned into Jewish consciousness. The God of Israel is not a God whose jurisdiction is defined by territorial boundaries.
Similarly, Rabbi Jonathan Sacks:
It was in the desert that the Israelites made a covenant with God and received the Torah, their constitution as a nation under the sovereignty of God. It is the desert that provides the setting for four of the five books of the Torah, and it was there that the Israelites experienced their most intimate contact with God, who sent them water from a rock, manna from heaven and surrounded them with clouds of glory.
What story is being told here? The Torah is telling us three things fundamental to Jewish identity. First is the unique phenomenon that in Judaism the law preceded the land. For every other nation in history the reverse was the case. First came the land, then human settlements, first in small groups, then in villages, towns and cities. Then came forms of order and governance and a legal system: first the land, then the law.
The fact that in Judaism the Torah was given bemidbar, in the desert, before they had even entered the land, meant that uniquely Jews and Judaism were able to survive, their identity intact, even in exile. Because the law came before the land, even when Jews lost the land they still had the law. This meant that even in exile, Jews were still a nation. God remained their sovereign. The covenant was still in place. Even without a geography, they had an ongoing history. Even before they entered the land, Jews had been given the ability to survive outside the land.
Somewhat relatedly, Rabbi Joseph B. Soloveitchik, Abraham's Journey:
We emerged as a people from the sand dunes of the Sinai Desert where we wandered for forty years. Why could we not rise as a people in our own land, in prosperity and abundance? the answer is simple. If our morality was to be one of kindness and hesed, it could not have been formulated for people who knew not what suffering is. Only people in exile could understand and appreciate a morality of kindness.
According to the Ari, the air of Eretz Yisrael was present with the people and moved with them -- wherever they went in the wilderness they had the air of Israel and of the beit hamikdash with them. So even though they were in the wilderness, it is like they were in Eretz Yisrael. So the torah could be said was given "in Israel", just not within its boundaries.
This can explain the gemoro in Shabbat 31b, which says that since they travelled because of Hashem's word, that was like the mishcan being built and taken down on the same spot. The gemoro there is discussing if one is chayiv on shabbos for pulling down a building, if you don't intend to build on the same spot. The gemoro brings a proof from the mishcan where the dinim of shabbos are learned from, where they pulled down the mishcan and didn't build it again on the same spot. On that the gemoro says, since they, the Jewish people, traveled because of Hashem's word, it is counted as though it was still built again on the same spot.
The meforshim have difficulty understanding this.Just because hashem was telling them where to go it should all be considered the same spot. The shvisas shabbos at the beginning of his sefer explains why it was considered the same spot. He brings an Ari that where ever they went in the desert they had the 'air' of erez yisroel with them and the 'air' of the bais hamikdosh. So the mishcan was always on the same spot of the bais hamikdosh. The gemoro in eiruvin 55b present daf yomi can also be explained with this theory, to explain why although they were living in tents in the desert, it is considered like living in a city 'bikviuss', since hashem was directing them. So the meaning of hashem directing them in the desert, is travelling with hashem, ie with the bais hamikdosh and erets yisroel therefore it was considered like living in a city in Israel.In other words although they were moving about in the desert they really were on the same spot all the time, this being no contradiction.
R Chaim Shmulevich adds that it's like a child with its mother. Even though the mother moves about, the child's place is with its mother. So even though they were in the desert it's like they were in Eretz Yisroel.
This also explains how they could make sacrifices in the desert and not in Israel. According to this explanation of the Ari it was given in the 'avira' the air of erez yisroel plus it also had the attributes of the desert mentioned in other posts. I must state the 'air' is not meant in a physical sense but a metaphoric one.
if they received the Torah in Israel , people might think that the obligation to fulfill the Torah is linked to their presence in the land . for that reason , the Torah was given as we were in transit , to foreshadow Jewish history which is a series of expulsions , exiles , wanderings, upheavals, etc. and to show that we are bound by the Torah in all circumstances .