Upvote:5
In the case of Moses, God spoke with him directly (e.g., the forty days on Mount Sinai) and instructed him in everything he was to write on the two stone tablets, as well as the scrolls of parchment.
In the case of the apostles, God gave them the Holy Spirit, and regarding the Holy Spirit, it is written (John 14:26),
But the Comforter, the Holy Spirit, whom the Father will send in my name, he shall teach you all things and bring all things to your remembrance, whatever I have said to you.
Upvote:5
In the time of Moses and before, history was recited orally, repeated and re-told, over and over, long before it was ever written down. In fact, as you read Genesis, you can hear the cadence of an oral telling. Even in the time of Jesus, most people did not read or write, and they relied on telling their stories and histories.
So, in addition to the Holy Spirit, as pointed out by H3br3wHamm3r81, the writers used their memories. The Holy Spirit is VERY important, but he used a people who were used to using their memories. We rely on books, electronics, photos, internet, and have very little understanding of what our minds could remember.
A more modern use of the oral tradition is found in the afterwards of the book "Roots" by Alex Haley. One of his sources was a historian in Africa who spent hours (days?) telling the oral history that he had learned, histories of peoples from that area, and a history that included the story of a young man who disappeared about 200 years before. He told names and events of many generations of people. In the same way, with the guidance of God, Moses wrote down stories that he had heard.