It is my understanding that P450 enzymes are capable of selectively degrading compounds that enter the cell from the outside (e.g. synthetic drugs) without damaging compounds that are metabolic intermediates normally found in cells. What is the mechanism that provides these enzymes with the ability to distinguish the foreign compounds that are substrates from the normal metabolic intermediates that are not substrates, if they are structually similiar?
The false premise in this question lies in the end of the last sentence:
…the ability to distinguish the foreign compounds that are substrates from the normal metabolic intermediates that are not substrates, if they are structurally similar
The assumption here would seem to be that the similarity between normal metabolic intermediates and foreign compounds is too great for discrimination to occur. There is no reason to assume this.
The substrate specificity of those P450 enzymes that metabolise xenobiotics is such that they will not metabolize normal cellular constituents.
There is a wealth of evidence that enzymes can have substrate specificity that is either extremely narrow or relatively wide. This obviously depends on the structure of the substrate binding site — its shape and size and the chemical nature of the amino acid residues there. Thinking in these terms one can envisage an enzyme that will discriminate against substrates with a single substituent at a particular position, but metabolize a variety of similar compounds that lack this. The evolution of P450 enzymes can be imagined as a process whereby mutations that allowed metabolism of toxic environmental compounds would convey an advantage and be selected for, but mutations that disrupted normal metabolism would be lethal (or disadvantageous) and be selected against.
The Broader context of P450 Enzymes
The question is posed in terms that might suggest that the only function of P450 enzymes is in removal of xenobiotics, and perhaps that they are only a feature higher organisms. In fact neither of these are true. Details of their occurrence in bacteria can be found in the Wikipedia article. However, a paper which I, as a non-expert, found interesting in this respect was published by Kawashima and Satta in PLOS ONE in 2014. This makes the point that there are two classes of P450 genes — those involved in normal cellular biosynthesis (e.g. steroids, cholesterols, vitamin D3 and bile acids), and those active against xenobiotics (e.g. plant alkaloids, aromatic compounds and fatty acids). Comparison of the genes for these in a variety of (mainly) vertebrates implies those involved in the metabolism of xenobiotics evolved from those involved in normal cellular metabolism, and such detoxification genes have arisen not just once, but on several occasions.