Originally, the activity site of most drugs was found by testing many, many substances and finding one that
has an effect. At that point, many were plant based, and of the hundreds of chemicals in the plant only one was having the effect.
As science advanced, the learned ti isolate and test the individual chemicals. Then they find out what it's chemical structure is, then fiddle with it to see it it gets better or worse. This is why there are "me too" drugs - a chemical is round that selects a site, Each company does its own research to gets it's own version of something like that which is different that what any other company has and patents it,
With time, better understanding of chemistry allowed them to get better at this process. using computer modelling of a receptor site and making a "designer drug" - a novel chemical that they hope hits the receptor they want. Again, they go through many rejects to find a good one.
Most recently, bio-pharmaceuticals have appeared. Using genetic technology, they modify organisms to produce the exact thing they want (like say, a particular type insulin).
Most drugs have no idea where their receptor is. They distribute throughout the body. But they only take effect where the receptor is. You take a pain pill, it affects wherever you are feeling pain. If only your head hurts, it still goes to your knee, It just has nothing to do once it gets there Vice versa if your knee hurts.