So this is a hugely important topic with minimal sourcing, and what sourcing we do have sometimes contradicts literary textual analysis, as a result you'll find a huge range of views on this question, which I'll try to sum up here, (sorry it's a bit long.)
The Quran is composed of suras, and after the opening Fatiha it is then arranged by length, with the longest suras numbering in thosands of words, and the shorter ones only a few dozen.
There are some descriptions of Muhammad's actual act of revelation which have been compared to seizure like states although there is much resistance to any sort of medical explanation for the origins of the Quran, and in any event that sourcing isn't that great.
Traditionally the Suras are dated to Muhammad's Meccan (i.e. early) or Medinan (i.e. late) period. Although the Al-Azhar dating is now standard in religious circles that's quite a recent development and there was much debate. Western academics who ignore the traditional sources have come, broadly speaking to similar conclusions about the dating. Certain stylistic features of Suras mark them as being either early or late. For instance if you exclude the openign "Bismallah" the early Suras do not use "Allah" to refer to God, but instead use "al-Rab" --The Lord.
This approach was most radically criticized by Richard Bell, who believed that the way the Quran was compiled, either by Muhammad or his secretaries, make dating whole Suras useless as the constituent components of the Suras come, in his view, from different times and include a significant number of glosses. So rather than a dating a sura as a whole his analysis of the Quran attempted to scrutinize it in very small pieces in figure out what had been inserted or altered later.
In his view the answer was "lots." He used a number of literary techniques to work this out, relying on changes in rhyme, meter, or syllable count between verses, changes in subject, and the accidental insertion of glosses by secretaries. So for him an individual sura evolved quite significantly, as the "units of revelation" were quite small and a sura was assembled from that.
At least some of this is accounted for in the traditional sources, as we know that certain verses were added, removed or forgotten.
But Bell's overall view, that Suras are primarily disjointed assemblages of small units of revelation that were assembled over time has been quite convincingly challenged by a number of academics in recent years. Much work has been done on "ring composition" in the longer suras, wherein repeated phrases ideas or units surround a central message or rule. Angelika Neuwirth and Michel Cuypers would be an example of people who have pushed back against the Bell hypothesis in the other direction. Cuypers book, at least in my view, probably goes too far.
The most balanced and in depth analysis that I've seen of the Quran's structure is probably in Neal Robinson's work. His book "Discovering the Quran" is excellent. He examines the rhetorical structures at work, noting, for instance, that where Bell found the oaths that open the early Suras ("By the sun and its brightness!" "By the fig and the olive!") to be basically incomprehensible gibberish that was tacked on later that actually the oaths as a rhetorical feature have bearing on the content of the suras. Oaths dealing with astronomical objects usually then accompany descriptions of God's power of creation. That sort of thing. But at the same time he does recognize that some of the features that Bell pointed out could very well be later insertions or glosses.
Now, to get to the compilation of the Quran, the question may be "inserted by whom?" Well, we know that some verses were inserted or removed by Muhammad himself. Then there the glosses I mentioned above. But although it's now widely believed that many or all of the Suras existed in written form of some kind the Quran as it exists today is what is known as the Uthmanic Codex, assembled by Uthman, the third Caliph after Muhammad.
Apparently the primary method for collection was reliance upon people who had memorized the suras (which was the general means of their conveyance) and using multiple people to verify the accuracy of the recitation. There are some traditional debates about whether in this process certain things were left out or purged, and about whether Uthman was sufficiently rigorous in his standards. We do know that it must have differed from at least some of the versions that existed because there was a minor rebellion by Quran reciters in Kufa against him. Uthman also collected and burned alternate accounts.
Part of this is where sourcing becomes a problem. Because we have no Qurans that are sufficiently old enough to prove the account of the Uthmanic collection there's room for doubt about that account. If there's ever going to be a better answer to this question it would probably require extensive archaeological work in Saudi Arabia, which is unlikely to happen. But the oldest manuscripts we do have, like the Sanaa manuscript, indicate that very old copies of the Quran from the 7th century generally have only minor differences from the current text.
Nonetheless some people like Patricia Crone and others, rejecting the traditional accounts and the accuracy of the quran reciters hold that it was a more fluid document which had extensive changes. Her anti-traditional, skeptical work formed a large part of the basis for Tom Holland's book on Islam which is popular but well outside anything resembling an academic description of the origins of the Quran.
Basically the consensus of academics and historians is, accounting for some changes (orthography, orders of Suras, and the like) that the traditional accounts for the origin of the Quran as a product of the revelation of the prophet Muhammad and then compiled into the text we have today by Uthman, that the traditional account I've described above is most likely correct.
A bit long, source wise, again, the best academic introductory book I'm familiar with, and one that addresses the various academic viewpoints is Neal Robinson's "Discovering the Quran."
TL;DR Revealed to Muhammad, lot's of debate over the composition and dating of Suras and whether Suras are unified or disjointed, written down by Uthman, deviant copies were destroyed, general consensus is that the Quran as we have it today very closely resembles the Quran as it existed at that time, but with broad academic discussion over whether that's true or not.