During the time of Prophet Muhammad sallalahu aalyhi wa sallam, the Qur’an was mainly preserved through memorization. Hundreds of Companions of the Prophet were huffaz (memorizers of the Qur’an) who had memorized the Qur’an without any mistake.
When Prophet Muhammad sallalahu aalyhi wa sallam, used to receive a revelation, he dictated it to a Companion, who wrote it down on anything that was available: bark, stone, bones, leaves, etc. The Companion would then read, what he had written down to the Prophet. If there were any mistakes, the Prophet would correct it and then let it be brought before everyone. The Prophet also described the order in which to record the verses; the Companions of Rasoolullah abided by his command and recorded it accordingly.