Oftentimes we have documents that talk about how things were pronounced, especially when they criticize people for how they talk (the Romans were rather famous for that). Texts like poems are also very helpful for knowing where words would have similar pronunciations or stresses, and can even help demonstrate sound evolutions despite spelling being the same. On a similar note, inconsistent spellings that suddenly crop in after years of consistency may represent a sound in flux, especially if they later stabilize with the innovated form. Many of those sound changes are relatively consistent across languages, which help confirm those observations (in the sense that we know condition A has resulted in innovation B in unrelated languages due to some known principal C). Finally, we have documents where someone from one language reports on a word from another language and talks about how it sounds in relationship to their language, or more simply a word that gets imported and brings its pronunciation along with it.
Taken together, there are generally enough pieces to put together to demonstrate how a language may have sounded with pretty high certainty. If there aren't enough pieces, though, there may be several pronunciations that are consistent with the evidence, and absent more evidence, it may be impossible to know which is the most accurate.