The word of Allah
The Quran is the speech of Allah, sent down to the Prophet Muhammad ﷺ. It calls itself a guidance for those who are conscious of Allah.
ذَٰلِكَ ٱلْكِتَـٰبُ لَا رَيْبَ ۛ فِيهِ ۛ هُدًى لِّلْمُتَّقِينَ ٢
This is the Book about which there is no doubt, a guidance for those conscious of Allāh -
How it was revealed
The angel Jibreel (Gabriel) brought it down to the Prophet ﷺ, and it came in parts over many years rather than all at once.
قُلْ مَن كَانَ عَدُوًّا لِّجِبْرِيلَ فَإِنَّهُۥ نَزَّلَهُۥ عَلَىٰ قَلْبِكَ بِإِذْنِ ٱللَّهِ مُصَدِّقًا لِّمَا بَيْنَ يَدَيْهِ وَهُدًى وَبُشْرَىٰ لِلْمُؤْمِنِينَ ٩٧
Say, "Whoever is an enemy to Gabriel - it is [none but] he who has brought it [i.e., the Qur’ān] down upon your heart, [O Muḥammad], by permission of Allāh, confirming that which was before it and as guidance and good tidings for the believers."
وَقُرْءَانًا فَرَقْنَـٰهُ لِتَقْرَأَهُۥ عَلَى ٱلنَّاسِ عَلَىٰ مُكْثٍ وَنَزَّلْنَـٰهُ تَنزِيلًا ١٠٦
And [it is] a Qur’ān which We have separated [by intervals] that you might recite it to the people over a prolonged period. And We have sent it down progressively.
Ibn Abbas described those years: thirteen in Makkah and ten in Madinah, around twenty-three years in all.
"Allah's Messenger (ﷺ) was inspired Divinely at the age of forty. Then he stayed in Mecca for thirteen years, and then was ordered to migrate, and he migrated to Medina and stayed there for ten years and then died."
How it was preserved
Allah Himself promised to guard the Quran.
إِنَّا نَحْنُ نَزَّلْنَا ٱلذِّكْرَ وَإِنَّا لَهُۥ لَحَـٰفِظُونَ ٩
Indeed, it is We who sent down the message [i.e., the Qur’ān], and indeed, We will be its guardian.
The Companions both memorised it and wrote it down. When Abu Bakr had it gathered into one copy, Zayd ibn Thabit collected it from what had been written and from those who knew it by heart.
"So I started looking for the Qur'an and collecting it from (what was written on) palme stalks, thin white stones and also from the men who knew it by heart"