Often we have some idea how they work and what the internal end result appears to be, but the pathways that the drug seems to work on aren't fully understood. So we know the drug does something to that pathway, but it is to some extent a black box for us. But the fact that the drug operates on the pathway also opens new avenues for the scientists to gain a better understand of that pathway.
Modern medicine is optimized for end results. If a drug appears to work, the "how" is of secondary importance. We can always figure that out later. Sometimes we find stuff in nature, for example, like chemicals produced by fungi or bacteria, and we discover that they have medicinal properties. They nevertheless get productized as drugs, even when we don't fully understand them or the pathways they target.