Every medicinal property we find in a plant traces back to the plant’s own survival.
These compounds evolved to keep the plant alive in its environment, and we just happen to benefit from them! Thankfully.
We call them secondary metabolites, and every one is super interesting in terms of WHY the plant has them, for example: bitter alkaloids taste terrible to a rabbit looking for a snack, some plants secrete terpenes into the soil to keep neighboring roots from crowding their territory and certain flavonoids absorb harmful UV-B radiation, working almost like a built-in sunblock (kind of)!
Nature wants to survive. Life finds a way to live. And it all comes down to phytochemistry, which I could nerd out over for hours!