Microorganisms and plants have been evolving for millions of years and along the way they've evolved to produce a range of substances that are able to do all sorts of things including killing pathogens
Instead of having to develop all of our drugs from scratch we've been able to take these substances from them and either use them directly as medicines or sometimes modify them in a lab of it first and then use them as medicines