In one sense, the answer is simple: they wrote down what they knew, through their own experience. For example, Luke says that since he had ‘carefully investigated everything’, he wrote down ‘an orderly account’ (Luke 1:3). In most cases, the writers were eyewitnesses who knew that what they had seen was so important that it needed to be recorded for others to read. John speaks of himself as one ‘who testifies to everything he saw - that is, the Word of God and the testimony of Jesus Christ.’ (Revelation 1:2). Sometimes, they were given a specific message to write down (see, for example, Deuteronomy 31:19, which explains where the words to Deuteronomy 32 came from, or Jeremiah 36), but more commonly they used their own words.
But Christians believe that in all cases, God carefully ensured that what these people wrote down was exactly what he wanted. ‘For prophecy never had its origin in the will of man, but men spoke from God as they were carried along by the Holy Spirit’ (2 Peter 1:21). We speak of Scripture being ‘God-breathed’ (2 Timothy 3:16), as being God’s Word. The writers of the Bible often referred back to older books within it as being God’s words - and most importantly, Jesus himself described the Bible in this way. In fact, he described it as God’s word to his hearers (see Matthew 22:31-32, which quotes Exodus 3:6).
Obviously, even the most brilliant poet or novelist cannot hope to write something that will affect readers for millennia, yet the Bible has done and continues to do so. We recognise it as inspired, because God worked, often behind the scenes (subconsciously, if you like) to ensure that the words we have in the Bible are his powerful, life-bringing words. Have a read of them for yourself, and see if they don’t strike you as astonishing! I’d try one of the gospels first: Mark is the shortest, and perhaps most dramatic, but the others are no less gripping.