You are correct that there are portions of the Bible that were written after the events they describe; sometimes, long after. For example, although Daniel is set during the deportation to Babylon much of the book was still in flux during the Maccabean crisis (167-160 BCE). On the other hand, there are parts of texts that are almost certainly older than the material that surrounds those older sections. Examples include the Song of the Sea (Exod 15), the Song of Deborah (Judg 5), the list of David's mighty men (2 Sam 23). The first 39 chapters of Isaiah mostly date to the prophet's lifetime, but underwent editing as well. I highly recommend picking up the New Oxford Annotated Bible (NOAB) if you can: it's a great resource and has helpful introductory material before books and sections (e.g., before Historical Books, Poetic and Wisdom Books, Prophets, etc.).
The Bible that you may hold in your hand, particularly the Hebrew Bible/Old Testament, has a complex history and multiple editors, some of them with different perspectives. While not perfect, the Documentary Hypothesis is perhaps the most widely known theory for how the Pentateuch (Gen-Deut) came to be. Put simply, it theorizes that four traditions were woven together by editors to make the first five books of the Bible. Those traditions are commonly referred to as J, E, D, and P. The J tradition is the source that uses the divine name Yahweh (Jahwe in German); E is for the source that prefers Elohim; D is for the Deuteronomist; and P is for the Priestly source. Again, it isn't a perfect theory, but is useful enough for explaining the idea of how multiple traditions became woven together.
What about 'historical accuracy'? I like how Marc Zvi Brettler put it in his forward to the Historical Books (Josh-Esth) for NOAB:
If we read [the Historical Books] as we read modern historical accounts we will misunderstand these texts in the most fundamental way. Many of these texts do contain raw material for a modern historian researching the history of ancient Israel from the late second millennium through the fourth century BCE, but this "real" history may only be teased out using sophisticated and complex tools--and even then, reconstructions are often extremely tentative. This is because the biblical historians wrote their accounts, sometimes using sources, to illustrate particular perspectives concerning the relationship between God and Israel.
So, there may be details and kernels of 'real' history related in the Historical Books, but the author(s) and compiler(s) were not intending to create a forensic history book that someone today might write. As Brettler notes, some cities were indeed destroyed in the time period associated with Joshua's conquest but the archaeological evidence does not support a wide-spread, rapid conquering of all Canaanite cities. The book of Joshua is not the same kind of military history as the many reconstructions of the troop movements on D-Day; rather, in Brettler’s words, it is “comprised of traditions that have been reworked very substantially over time in order to convey a particular picture of God and to justify the territorial claims and aspirations in ancient Israel” (Ibid.).
The New Testament isn’t my speciality, but I’ll give a brief overview. The earliest NT texts would be the authentic Pauline letters, with Phil 2:5-11 being seen as one of the earliest Christian hymns. Scholars generally name Mark as the earliest of the gospels (ca. 66-73 CE). Revelation was possibly started as early as 64 CE and would have been completed by 96 CE at the latest. The other gospels and letters are more difficult to pin down.
So, in brief summation, most of the HB/OT was created by weaving together multiple strands of tradition with the purpose of saying something specific about God and Israel. Although there are very old portions of text and pieces of 'real' history, the HB/OT is less concerned with forensic history than it is with God's faithfulness. The NT was written in a much shorter time-frame overall, though the earliest books are still some decades after the crucifixion.