Some drugs are metabolized in the liver or the kidney to different molecules. Other drugs are excreted unchanged. The metabolites that different drugs may change into are all known - and therefore presence of metabolites in blood or urine is what would be used to prove use.
What typically would make a drug difficult to detect is a short half-life, which means it is rapidly eliminated in the body. This reduces the window in which detection can occur. For example, cocaine is only detectable in urine for 3 or 4 days typically whereas marijuana may be detectable for weeks.
Drugs that are perfect for doping are those that are eliminated rapidly from the body and have effects that persist after cessation - so that the athlete can use, reap the benefits and then cease using in advance of anticipated testing while maintaining the benefits.
In some cases testing doesn't exist yet or is costly or otherwise impractical. New compounds are created all the time and it is tough to keep up. Those who use substances will know how long it is detectable and should then be able to quit in time to avoid being caught. Those who get caught metaeither fucking morons, miscalculated, or were genuinely caught off guard by the test. As testing is governed in collective bargaining agreements in many professional sports, often athletes will either have sufficient notice of a test or will know how many tests per year they will receive, thereby giving them a free pass to use after they've hit their limit.