Memory, writing, and checking every narrator.
The Companions memorised what they heard and saw, some of them wrote it down, and they taught it to the generation after them. As the reports spread, scholars began to ask of every one: who told you this? They recorded each narrator's name, memory, honesty and teachers, and accepted a report only when its chain stood up to that scrutiny.
From this work came the great collections. Among the best known are those of Imam al-Bukhari (d. 256 AH), Imam Muslim (d. 261 AH), Abu Dawud (d. 275 AH), at-Tirmidhi (d. 279 AH), an-Nasa'i (d. 303 AH) and Ibn Majah (d. 273 AH). Centuries later, Imam an-Nawawi (d. 676 AH) selected forty-two hadith from these and other books, each one a foundation of the religion. That small book, the Forty Hadith, is where this path takes you next.