Question: "How do we know when the books of the Bible were written?"
Answer: We have a few basic ways of knowing when the individual books of the Bible were written: a combination of internal and external evidence and, particularly in the Old Testament, traditional accounts.
Internal evidence might consist of the style of writing and mentions of people or places who can be more precisely dated. For example, while the book of Ruth is set during the time of the judges, scholars place the literary style as that of the time of the Israelite monarchy—the Kings—based on other writings more accurately dated to that time. The mention of David (Ruth 4:17, 22) also implies a date some time after David's reign.
Another example: the book of Daniel uses a literary style and specific Persian and Greek words that place it around the time of Cyrus the Great (ca. 530 B.C.). Linguistic evidence from the Dead Sea Scrolls gives us authentically dated examples of Hebrew and Aramaic writing from the second and third centuries B.C., when some claim Daniel was written, and it does not match that found in Daniel, which was written in the sixth century B.C.
Other internal evidence might be the concerns the author is addressing. For example, the two books of Chronicles tell the history of the Jewish people and how they came under God's judgment in the form of the exile to Babylon. Traditionally, scholars have believed Ezra to be the author of these books, because the following two books, Ezra and Nehemiah (also written by Ezra), deal with the return from exile and the need to be obedient to God's law, and they are written in nearly the same literary style.
The date of that return, which began under Cyrus the Great, can be correlated to historical records outside the Bible that place his reign from approximately 559 to 530 B.C. The dedication of the new temple in Jerusalem, in 516 B.C., is corroborated by the records of Darius I, and a second return of exiles was allowed under Artaxerxes I, whom we know ruled Babylon from 465 to 424 B.C. All these things help us to closely place the writings of those particular books of the Old Testament. Biblical scholars use similar cross-referencing to date other books of the Old Testament.
In the New Testament, books are generally dated by the concerns being addressed, e.g., the growing Gnostic heresy, and how much they quote from other New Testament writings and a cross-referencing of events such as the collection for the needy in Jerusalem discussed in Romans and 1 and 2 Corinthians. We also have historical, extra-biblical accounts such as that by the Jewish historian Flavius Josephus to corroborate events described in the Bible.
The Gospels are often dated by something that is not mentioned: Jesus predicted the fall of Jerusalem in Matthew 24:1-2, and we know from historians such as Josephus that the city fell in A.D. 70. It seems logical that if such a prominent prophecy had been fulfilled before the writing of the Gospels that it would have been mentioned, as is the fulfilled prophecy of Christ's resurrection as found in John 2:19, 22.
It's important to note that even among scholars who believe the Bible to be God's inspired, inerrant Word there is some disagreement as to the exact dating of the biblical books. A good study Bible such as The NIV Study Bible or a commentary will lay out the various lines of evidence for the dating of the books.