This is a broad question that volumes have been written on. Before going to a summary of the mechanics of how it came about let me say first that ultimately the reason the NT is complete and inspired is found in 2 Pet 1:20-21 “But know this first of all, that no prophecy of Scripture is a matter of one's own interpretation, for no prophecy was ever made by an act of human will, but men moved by the Holy Spirit spoke from God.” Down in the same context Peter compares creation “grass withers and fades away” to the Word and says “but the Word of the Lord endures forever.” It is God that superintended making sure it was put together just as He designed, and it is the Lord that will see to it that the Word continues to survive. Everything about the mechanics then are under the understanding of the sovereignty of God moving in time and circumstance to create His Word, and then maintain its integrity. With this ultimate understanding, how did God establish the New Testament? Here is a summarization of the major keys for authentication:
-Christians gathered together at church councils in the first several centuries A.D. for the purpose of officially recognizing which books are inspired. Note – it was a recognition process whereby they recognized what God had already determined. The recognition tests followed this path:
-Is it authoritative (“Thus saith the Lord”)? – 2 Cor 2:17; 2 Cor 5:19; 1 Thess 2:13
-Is it prophetic (“a man of God” 2 Peter 1:20)?- A book in the Bible must have the authority of a spiritual leader of Israel (O.T. – prophet, king, judge, scribe) or and apostle or associate of an apostle of the church (N.T. – It must be based on the testimony of an original apostle).
-Is it authentic (consistent with other revelation of truth)?
-Is it dynamic – demonstrating God’s life-changing power (Hebrew 4:12; James 1:18; John 17:17)?
-Is it received (accepted and used by believers – 1 Thessalonians 2:13)?
The New Testament canonization
-The apostles claimed authority for their writings – Colossians 3:16, 1 Thessalonians 5:27, 2 Thessalonians 3:14.
-The apostles writings were equated with the OT Scriptures – 2 Pet 3:1, 2, 15, 16; Eph 2:20.
-The writings of the NT were authenticated by each other – 2 Pet 3:16; 1 Cor 15:4.
-The last in order of the books of the NT provided absolute closure to the canon – Rev 22:18-19; as well as the language of Jude 3; Eph 2:20.
-The council of Athenasius (AD 327), and Carthage (AD 397) confirmed the 27 books.
Conclusion: God has moved in history to reveal Himself (Heb 1:1-2). For the Old Testament, God moved in history through the Jewish nation. In the New Testament God moved providentially in time and history through the early years of the church guiding and directing, providing key leadership primarily through the apostles in actually writing His Word (John 14:16-17; 16:13; 2 Pet 1:20-21). This was followed up providentially through His people as they displayed wisdom, and using all appropriate steps of examination and verification – the early church (in the first few centuries) identified and accepted the books of the Bible as it is now formed. This was largely an undisputed recognition among orthodox scholars and believers with unwavering near unanimous conviction. This was clearly under the guidance of the Holy Spirit.