There are two parts to this answer. One is statistics, the other one is chemistry/medicine.
Statistics: a test for a substance uses a certain sequence of processes, materials, human capital etc. in this process, things can become contaminated, not only by manipulation as suggested by others here, but also just as part of the process. Think about wearing a white shirt, but maybe during the day somehow you got a stain on it and when they hold that small piece of your shirt under the microscope, they will say "hey, this is brown, not white" (positive A sample) and then someone checks a different part of your shirt without the stain and they say "nvm, it's white" (negative B sample). There are a million ways the stain could have gotten there. So the test relies on probability - it says "ok, given what we know about white shirts, our process is so good that in 99% of cases we correctly identify a white shirt". And to a scientist, that's fine. To the very very rare athlete with the small stain, it's tough. And this goes both ways, there can be false positives or false negatives. The more sensitive you make the test to detect stains (i.e. get a crazy good microscope) the more likely you will find even the tiniest stains (traces of doping). But the more sensitive it is, the more likely it becomes that you zero in on a tiny tiny stain that really does not say much about the shirt as a whole. You can look this up in scientific terms: specificity and sensitivity.
The second is medical science and depends on the substance: often, the test does not detect the actual substance, it detects certain proteins or other biochemical structures that are associated with that substance and concludes that it must be the substance. EPO tests have been criticized for this by cyclists (which is a dirty sport, so take with a grain of salt, but there is at least some scientific evidence from the lab to back it up). And there can be other substances that shouldn't be in a human body but that have the same or very similar proteins as the doping substance. We don't eat clean, our food/water is treated with chemicals etc., so the test might not be able to distinguish between different sources of that particular thing it's looking for (not "monospecific" is the term for it). And then sometimes they use more/different testing methods on the B-sample to rule out that this happened and find that with the other method for the same substance it's negative. So yeah, it can be legit.