How does the body "know" how to metabolize foreign substances
One strategy for xenobiotic metabolism seems to be making a lot of similar enzymes with different substrate specifities. Lets take the cytochrome p450s as an example. These are a class of enzymes with 57 genes identified for humans, and I don't know how many alternative splicing products are made from those genes. As a class, they are responsible for about 75% of drug metabolism and are predominantly found in the liver. They work by using a heme group with a carbon monoxide to add oxygens to xenobiotics. Here is a diagram of the mechanism:



It's been too long since my drug metabolism class to go into more detail on these enzymes, but there are other classes of enzyme that fit this multiple enzyme strategy such as the 2 monoamine oxidases, 5 flavin containing mono-oxygenases, and 4 epoxide hydrolases. So the body doesn't really know how to break down xenobiotics, but has a wide array of enzymes with broad and overlapping substrate specificities that work on whatever they happen to bind to, whether it's an endogenous hormone or pharmaceutical compound or toxin.