“prophets, though human, spoke from God as they were carried along by the Holy Spirit.” 2 Pet 1:21
Jesus was crucified and raised from the dead in either AD 30 or 33. By AD50, the church had grown enough that some of the leaders of the church needed to write to other bits of it (Galatians written in 48, 1 Thessalonians in 51, James maybe even earlier). The churches found these letters so valuable that they made copies of them, and circulated them to other churches as well, and reading them alongside the Old Testament. Even by the time 2 Peter was written (mid-late 60s?), people were evidently reading Paul's letters as Scripture (2 Pet 3:16).
Also in the 60s, the apostles started to realise that they were probably going to die before Jesus returned. Some of the apostles who had known Jesus (Peter, via his friend Mark; Matthew; John) wrote down accounts of what Jesus had done and said. They'd already been preaching this for 30 years; it's like writing an account of the Falklands War today, using the accounts of soldiers who fought there – the same sort of timescale. Books weren't in common use yet, but the early church very quickly created a scroll of the four gospels, which almost every early Christian church seems to have had access to, along with another scroll of the Letters of Paul.
For the next few hundred years, Christianity was illegal, and often persecuted. Printing, of course, hadn't been invented, and so churches tended to have a collection of scrolls of New Testament writings which they used. Besides Paul and the Gospels, they might well have some of the other NT letters (Hebrews-Revelation, especially 1 John), and maybe some other books like the letters of Clement, Bishop of Rome in the 90s.
In the 100s, lots of false teachers arose, just like Jesus said they would. Some of them (e.g. Marcion) tried cutting bits out of the Bible because they wanted to cut off any hints of Jewish roots. Others (e.g. the author of the Gospel of Thomas) wrote fake “gospels” to try to add things into the Bible which fit their own agenda. Irenaeus, bishop of Lyons in about 180, wrote about them and pointed out that the church had always had the 4 gospels and that they shouldn't accept any of the new nonsense. [If you want to, why not try reading some of these fake “gnostic” gospels – they're very different to the real thing, and can be quite funny in an awful way. They tell us far more about what the authors liked (secret knowledge, petty magic tricks) or didn't like (women) than about Jesus though.]
When Christianity eventually became legal, in the early 300s, the church started to compare notes on which books they had, and which ones were Scripture. Interestingly, they didn't discuss it at the Council of Nicea, which was the first big Christian get-together after Christianity was made legal – there were more pressing things to discuss like Jesus being God and the date of Easter. It wasn't so much a process of deciding which books were Scripture as recognising. If I pick someone out of a police line up, I'm not deciding that that person robbed my house; I'm recognising the person who did it.
The criteria the Church used were roughly:
- Is it either by an apostle (leader of the early church personally commissioned by Jesus – Peter, John, Paul, James, etc), or was it approved of by an apostle (Luke, Mark)?
- Does it fit with the rest of the apostolic teaching? (“Acts of Paul” was ditched as it clearly wasn't by Paul because it didn't fit with the rest of his teaching.)
- Has it been used by a lot of Christians, and tested to see that it has the effects that we expect Scripture to have?
Using these criteria, Athanasius, bishop of Alexandria, wrote the list we've got today in his Easter Sunday letter of 367, and this was ratified by the Council of Carthage in 397. It's easy enough to find copies of the books that they decided to leave out – Shepherd of Hermas, 1 Clement, the Didache, and so on. They don't add much.
There's a different question, about how sure we can be that the New Testament we read today is what was originally written. Ian Paul, who knows more about these things than me, has some helpful comments.
As a consequence of all this, we can be very sure that what we have as the New Testament now is pretty much exactly what those who knew Jesus personally were saying and teaching about him in the Early Church.