Honestly? Sometimes we don't, and when we don't we're usually pretty open about it.
First and foremost, there is something called the historical method. It's basically a set of common rules for how to evaluate and interpret historical data. I will not go into depth about it, there is a pretty decent wikipedia article about it and I can recommend great books if you want to get into it for real, I'd start off with From Reliable Sources: An Introduction to Historical Methods by Howell & Prevenier.
I will also direct you to this theory thursday thread where /u/commiespaceinvader asks essentially what you're asking and /u/sowser launches into a massive wall of text I will not even attempt to summarize for fear of misrepresenting what I feel is a solid arguement.
If you want a more simplistic explanation I can give you that too. I'm going to use a post I recently made to illustrate some things. Here is my post on why the Kievan Rus (or rather Vladimir the great) choose to become eastern orthodox rather than Muslim or remain Pagan. I think it's an interesting case because it's one where we have a wealth of archeological evidence. It is also interesting because we have a lot of written sources but theses sources present two partially conflicting narratives.
Certain as we can be
So what do we know? Well for starters we know with as much certainty as we ever will that the Kievan Rus became christian under the rule of Vladimir the Great. We know this because all sources agree and it matches the archeological evidence. Grand churches were built, pagan shrines were suddenly not built. We start seeing graves with evidence of orthodox christian funeral rights and so on. So on this point we are pretty damn certain. We also know the overall timeline of the general region pretty well. We know the reigns of Byzantine Emperors both from Byzantine chronicles and from non-Byzantine sources. This is reliable evidence, essentially all sources, present and contemporary, agree that this happened and we have no common-sense reason to distrust it. It is also in many cases (such as physical evidence) considered highly reliable. From a historical point of view, archeological evidence and multiple sources all agreeing is as close to certainty as we will ever come.
Source criticism and conflicting sources
What we don't know is the reason for Vladimir's choice. We will never know this with complete certainty, as is always the case when it comes to the actions of humans. We can guess, we can extrapolate, we can assume, but we can never truly know. It is possible that the narrative of The Tale of Bygone Years is true, that Vladimir was so impressed with the account of his ambassadors that he decided there and then that Eastern Orthodoxy was the way to go and that his marriage to Princess Anna was merely a result of his bold negotiations. There is however good reason to question that narrative.
Why? Because it doesn't match that of non-Kievan sources. The Tale pf Bygone Years (aka the primary chronicle) is not am impartial source. The surviving versions of it were edited by the head of a Kievan monastery under the patronage of Vladimir Monomakh, the then ruler of the Kievan Rus and by an unknown but probably Greek person of education (because he evidently knew much more about the timeline and history of Byzantium than the editor of the other version did). In either case, both editors had a direct interest in establishing the lineage of their respective patrons, Vladimir Monomakh and Mstislav the Great respectively, to that of Vladimir the Great and therefore also in glorifying Vladimir the Great.
Does this make The Tale of Bygone Years useless as a source? No, but we have to keep the bias in mind. We can still use it to see which parts match accounts written by people who had no interesting in glorifying Vladimir and we can also with some degree of certainty assume that parts which are not glorifying either of the Vladimirs or Mstislav are true simply because no one had an interest in distorting those parts.
This type of analysis is called source criticism. The last part here is important because it's an example of weighing individual evidence independently, regardless of the overall trustworthiness of the authour or source. Source criticism is important and it helps us in the second part of this, dealing with contradictory sources. The Tale of Bygone Years does not match the accounts given by numerous outside sources. Non-Kievan historians, both muslim and christian, give at least four other accounts that dispute the account of The Tale of Bygone Years. Since they are four independent sources with arguably greater credibility and in at least three of the cases by actual historians (as opposed to the two versions of The Tale of Bygone Years which were written by a monestary head and an unknown author) we have good reason to think that this is a more reliable account of what happened.
When we really don't know for sure
Notice the account of the third Bardas-boys rebellion in my post? The one where they rebell against Basil II together? We don't know where the final battle or battles took place, nor do we know exactly when. We know approxiamately because we know the reign of Basil II, we know the rough timelines of the people involved and so on but sources don't agree on exactly where or when and we have no real archeological evidence either. That's when we just say so. The only thing sources are pretty much in agreement about is that Bardas Phokas died after a failed charge against Basil II and that the rebellion failed quickly thereafter. Since that's all we know, that's all I will write about it.
I hope this helped to shed some clarity on how historians work and to what degree we can and can't be certain about history.
EDIT: Spelling, user names etc
Contrary to what is commonly assumed, history isn't entirely subjective. History is an academic discipline that relies on thorough research. Historians have to employ proper usage of historical methods and practices while studying their sources. This ranges from cross-checking their sources to accurately vet the information they've gathered, to employing reflexitivity and self-awareness of their own frame of mind. Most of all, historians need to take the specific context of their sources into account. There are various types of sources. They came into existence for different reasons and were written by different authors with different motivations. They must therefore be analyzed differently.
Let's say that you've found extensive records on the bussiness logs of debt collectors in a certain region. The only information within these sources is quantitative information written down for practical reasons. You have no reason to believe that this information was written down for any other reason than for the sake of keeping a bussiness inventory. At that point, it's very reasonable to assume that this information is accurate, bar the individual errors. However, the only information you can gather from these sources are some names and numbers. While these are certainly sources worthwile of study, you'd ideally want to combine them with other sources in order to provide a context from which you can draw some conclusions.
Let's say that these sources cover a good twenty years and that you've noticed a significant increase in acquired debt during this time. This intrigues you and you want to know the context behind this increase. You write down some of the more promiment names within these sources and you look through local or regional historical archives. You're in luck and you manage to find some diaries and letters written by the debtors mentioned in the sources you studied. At this point, you can look through these new sources to see if you can find anything related to the increase in debt. However, these are qualitative sources which weren't created purely for practical reasons. SO you have to approach them differently. You'll have to carefully analyze the information from these sources and be constantly aware that these were written by individuals, each with their own personal motivations and subjective experiences.
When you find a reoccuring reference in these letters or diaries to a flood prompting the individual to accrue more debt, something that corresponds with the dates and numbers of the first source you studied, that's when you've struck gold. You now have multiple sources, created for various reasons, with a high degree of credibility, all corroborating this event which led to some individuals accruing more debt! This is the start of a historical thesis. Now, this is obviously a very simplistic example. This is just to illustrate how historical research generally happens. In reality, historians usually have to be more creative, use more sources and secondary literature to create an academically sound historical thesis. This example obviously can only occur when there's a huge reserve of available sources. This isn't always the case. It heavily depends on the region and period of time. Very often historians look to interdisciplinary research for answers or affirmation of their findings when they lack sources. Some of the most common interdisciplinary academic disciplines are archeology, anthropology, sociology, etc.
So, as you can see, historians are trained to analyze their sources properly. We are taught that there are different types of primary and secondary sources. We have to know the different types of information and how to interpret them properly. We also have to be very wary of the context surrounding the sources, such as the motivator behind its existence or the motivations of the author in question. We also have to take heed of various reasoning pitfalls that come with the interpretation of historical sources - such as presentism, teleological thinking or being unaware of your own frame of mind. These are all aspects of writing history that are either taught or become self-evident through studying history. So historians don't just write history willy-nilly, there's a lot of theory and practice that needs to be taken into account when writing history academically.
I'd like to end this comment by saying a few words on what constitutes "fact" or "knowledge". I feel like it's a common trope these days to claim that social sciences are entirely subjective as opposed to the factual nature of the hard sciences. There really is no such chasm. What constitutes our contemporary knowledge is mostly created through academic consensus. This is true for any body of knowledge, especially at an academic level. We determine what is factual through agreeing to a shared reality. While it's true that a discipline like history will unavoidably contain a multitude of interpretations and analyses, it's still based upon an agreed factual experience of a shared reality. The diverging interpretations don't subtract from that, they add to it.
I'd urge you to read through the sidebar on this subreddit if any of this interests you!