The honest answer is: you don't know, and thus the phenomenon of multiple discovery. Sometimes this is simultaneous, and other times it is independent, as for example in the case of conductive polymers, which were apparently rediscovered and lost several times.
Now, in practice, you must do a reasonable literature search, and part of the training that one receives over time is how to figure out how large a reasonable literature search is. It also helps to participate in conferences, where one becomes generally familiar with the thinking and knowledge in one's field. Submitting one's own work for peer review and presenting it in talks also offers opportunities to be informed of connections that one was not aware of.
While this may seem embarrassing, in fact, as long as one is doing one's reasonable due diligence, it is not worth being embarrassed about. This is because it's very rare for rediscovery to happen in the same context. There's a staggering amount of literature out there, but the number of problems is even bigger, and so any given specific piece of work may actually have startlingly few people in a position to conduct it.
Thus, rediscovery in practice often heralds a new connection being made between fields. In this case, the recognition of rediscovery often opens up the "unaware" side of the interaction to be able to import and adapt results that were based on the prior work, thus rapidly advancing the new area in which those results are being applied.
In short: do your due diligence, but don't fear discovering you've reinvented the wheel.