There are two main ways to determine when a Bible book was written: internal information and external information.
What methods help us figure out when the books of the Bible were written?
Internal evidence can involve style of writing, people, places, and events mentioned in the book. For example, Daniel uses Aramaic language in some places, narrowing its writing to a time when Aramaic would have been a common language among the Jews. He also wrote from Babylon, indicating a particular 70-year period during which the Jews had been deported to Babylon. Internally, he also mentioned the exact times when certain kings reigned and which year he recorded certain events to help narrow the time period in which the book was written. Of course, since the book was written by Daniel, it also had to be composed during his lifetime.
External evidence can also be used to help date when a book was written. For example, the New Testament books were all written following the death and resurrection of Jesus. These events took place around AD 30—33, meaning that all 27 books were written after this time. The end date of these books also must have taken place by the time of the deaths of the writers. Tradition holds that Revelation was written by the apostle John, the last living apostle of Jesus, as the last book of the New Testament, near the end of the first century. Therefore, external evidence shows all 27 books of the New Testament were composed between AD 30—96.
Specific books can be determined within a much smaller window of time. For example, the apostle Paul died sometime in the mid-60s, meaning all of his 13 writings were finished before this time. In particular, his prison epistles were written during the two years he lived under house arrest in Rome, in approximately 60—62. Based on this information, Ephesians, Philippians, Colossians, and Philemon were each written during this time period.
In some cases, external evidence can help show that one book was written before another without providing the exact time. For example, Luke was written as the first part of Luke-Acts. Since Acts concluded with Paul's two years in Rome, Luke was likely written just before Acts by the mid-60s. It may have been researched some time before and was completed in Rome sometime before the persecution of Christians related to the fires at Rome in AD 64.
Of course, even serious scholars of Scripture often disagree on the dates of some biblical books. Date of writing for some books is easier to determine than it is for others. Commentaries and other research tools can help provide evidence regarding the date each book was written. Even when there is some disagreement regarding the date of a Bible book's writing, this does not mean that the content of the book is in dispute: "All Scripture is breathed out by God and profitable for teaching, for reproof, for correction, and for training in righteousness, that the man of God may be complete, equipped for every good work" (2 Timothy 3:16-17).
When were the Gospels written?
Were there different authors of the books of the Bible? Who were the authors?
What is the basic timeline of the Old Testament?
What is the basic timeline of the New Testament?
The Dead Sea Scrolls - What are they and why do they matter?
Truth about the Bible