Well, he was illiterate according to tradition, which developed some years afterward. He is described in the Qur’an as “unlettered” but there is some scholarly debate about what exactly that means in that context. It may refer more broadly to Muhammad coming from a community that did not have the (capital-B) books, or the scriptures. It may also refer to partial illiteracy. There is also a minority tradition among Islamic scholars that he became literate after the Qur’an started to be revealed to him.
Based on what Muhammad did in his early life in business, I would say the odds are he had some sort of practical literacy. Here is an interesting take on the matter. The problem (with this as well as a lot of early Islamic history) is that there is still so much that we just don’t know yet. We are still dark on how much literary activity was around Arabia pre-Islam, with more work I’m aware of focusing on the great poets of that time.
Expanding on this a little, there’s a kind of annoying trend in popular Islamic discourse that tends to over-exaggerate how primitive society was at that time in order to emphasize the miraculous nature of the Qur’an. This has gone hand in hand with the increasingly popular proliferation of scientific miracles of the Qur’an, which hold that the Qur’an (and also hadith) are full of scientific facts and information that could not have been known at the time of its writing. Popular examples of this concern advanced embryology, mountain formation, the speed of light, etc, all supposedly in the Qur’an. So “How could Muhammad, an illiterate desert man, have made up something as amazing as the Qur’an?” is a way of illustrating simply the doctrine of the inimitability (Ijaz) of the Qur’an. It’s a part of the general impulse that motivates the above two trends as well. The extent that it is historically true is still debated.
The trend of finding “scientific miracles” is one that dates pretty closely to the modern era, for obvious reasons, (Classical Muslim scholars tended to emphasize the continuity of the Qur’an with ancient science, though you can see interesting parallels from that time in the transformation of the “splitting of the moon” from an eschatological future sign into a historical miracle) while the “awful backwardness of the world pre-Islam” idea has roots in long-held Islamic ideas of the general enlightening of Arab peoples that took place with the appearance of Islam. Neither trend is necessarily illustrative of Muslim scholarship or consensus at any time, especially the scientific miracles trend. As popular as it is in some circles, I don’t know any serious Islamic scholar who would press that point in an academic setting.
Ok, too much. Now to the OP. I previously wrote a long post on the subject here of new scholarly investigations of who wrote the Qur’an, and it is still a young field that has a LOT of work to do.
Traditional views of the Qur’an’s development (taken from Islamic tradition) that emphasize unitary authorship and a purely Arabic creation context have been challenged a lot recently. Most suras seem to come from the right time and context, but the development of an official canonized Qur’an incorporating some of these fragments of verses has a lot to do with the flowering 200 years after the death of Muhammad of Qur’anic exegesis and Islamic Jurisprudence based on Hadith, which were broadly compiled around this time.
Analyzing verses of the Qur’an has led some scholars to both classify them as serving different functions in a proto-Islamic liturgical framework, as well as drawing inspiration from earlier Christian works that were likely written in Syriac-Aramaic.
Where did it come from? I’d say a lot of it was liturgical recitations that developed as a gradually more distinct sect emerged and gained independence, with quite a bit of historical inspiration from diverse sources (Not just Jewish and Christian, but also Persian and from ancient Greek knowledge as well). I’m not sure if borrowing is always the right word, since the Qur’an does understand itself as a text, as a distinct Arabic Qur’an, that is in conversation with texts that came before it like the Bible. But I guess borrowing is fine, I’m blanking on a replacement.
The Qur’an was made to a particular people in a particular place, and we see lots of exhortations to a community concerned with present day events as well, that would have been made on the spot. Leaders after Muhammad, most famously Abu Bakr and Uthman, did much to standardize the Qur’an’s form(s), and Islamic jurists a couple of hundred years afterward really made it official, and so we have the Qur’an today, in several different forms, based on different pronunciations of the recitations. (The early Qur’ans were written in a script that did not have vowels, and it was standardized based on the Quraysh (tribe of Muhammad) dialect, but… well, it’s a long story. How much difference there is between these different recitations (10 are accepted, plus I think 7 are kind of ok) is something I am totally not qualified to speak on. Here’s a thesis on the subject)
Hope that helps, I can dig out my textbooks if anyone has questions or sees something I got blatantly wrong.