by a process called (Wahy) and then the prophet PBUH was saying the versus which are received and some of our prophet PBUH friends (Sahabah) god bliss them were writing them on a piece of old sking or paper if available so they do not forget it.
After prophet PBUH died his friend Abu backer god bliss took over him and Omar God bliss suggested to Abu backer to gather the Quran in one book and the suggestion was accepted.
This is as far as I know and I hope it explains some things to you.