Good question Jaffa!
Right, well, I'll do my best.....may be somewhat flawed!!
The first five books of the OT, The Pentateuch, were often attributed to Moses and the story of the children of Israel, and accepted by Jesus as scripture, as were the Psalms, the Prophets and the wisdom writings (Proverbs, Lamentations, Ecclesiastes) so we know these were around and accepted.
The NT is slightly more complicated. It does seem that by the time of Paul some of the gospel writings (largely Luke who he mentions) were in use by the early church as scripture, and basically the rest was gathered together over the next three to four centuries. You may think this somewhat trial and error-ish, like how could that work over so long, but from various early christian writings (Ignatius of Antioch IIRC, and also Polycarp, then later Constantine etc after conversion) it seems that certain books were used by the church as a whole and therefore accepted across the board, and these books crossed over and came together with each other over time.
Then, in the 4th century there were various councils of Christian leaders/bishops (Laodicea was one I think) and they came up with a definitive list. It wasn't random at all, they actually applied various criteria such as whether it was accepted by the larger church and whether the writings seemed to reflect the teaching of Christianity and were inspired by the Holy Spirit. It seems there was prayer over the contents. That was when it was decided that the 39 OT books and 27 Nt would be included.
There were various Apocryphal writings left out. Some of these are included in some versions of the bible (NRSV, Jerusalem Bible) but are not accepted as 'canon' (Scripture) but as useful writings for one reason or another. I've read most of these as they are interesting but funnily enough never felt that spark, but that's hardly objective when it comes to the whys and wherefores of it all. In the end it comes back to faith and trust: Do we trust that God inspired these people to put the writings in God wanted God's people to follow and live by?
As for later writings like the Qumran stuff (dead sea scrolls) and Gospel of Mary, well the Qumran stuff reflects something of what is in already, doesn't contradict it, and Gospel of Mary seems to be a piece of Gnostic writing, the Gnostics were around in the first century and later and saw the way to God through knowledge, didn't believe in the resurrection etc. I'm no expert on the Gospel of Mary at all but I believe there are some questions as to A) it's authenticity in general and b) whether it was written by the original Mary. In all, it seems it wasn't 'meant' to be in the bible, but all of these writings can be used in many ways and shouldn't be discounted.
I am waffling hugely, will try and find some articles that put it all better and more succintly/correctly!!!