Let's say if there was a large field with both poisonous flowers and non poisonous flowers. There are no animals in that field that eat flowers. But if we would put animals in the field that eat flowers, all of the non poisonous flowers would die out. Why? Because the non poisonous flowers couldn't protect themselves and kill the animals because they didn't have any poison to kill them. However, the poisonous flowers wouldn't die out because they were able to kill the animals with their poison and the animals realized that if they eat the flowers they would die. That's why there are more poisonous flowers than non poisonous because the animals realized they'll die if they eat the poisonous flowers but most of the non poisonous will become extinct. That's why there are more poisonous flowers.
It's called ' natural selection '.
Example of how Natural Selection Works (Just a made up example):
A rodent family is living in an enviroment that's on the cool side. Their short fur doesn't help them keep their warmth either.
One day the mother gives birth to 12 babies. One of the babies for some reason was born with a bit thicker fur than the others.
Because this certain rodent had longer hair he grew up having special advantages. He was able to handle colder whether because of his thicker fur which meant he was able to spend more time finding food. Because of this his life lasted longer.
When he reproduced his babies had thicker fur as well. And since they had thicker fur they all lived longer.
Because they lived longer they were able to reproduce more and they all reproduced babies with thicker fur. As the thicker fur rodents populated more the shorter fur rodents slowly died out because they didn't have as great advantages as the thicker fur rodents. They didn't live as long because they couldn't keep as warm which meant that they couldn't reproduce as much.
So in the end this short haired species became a thick haired species.
And one day another rodent might be born with something like....longer legs. So they could run faster away from predators and hunt better. Since these qualities give this rodent an advatage it will live longer and pass it down to his kids and you know how it ends.
Over millions of years these animals could change quite a lot. making them an entirely different species.
To prevent from being eaten.
If you were a plant, you'd probably want to evolve a system to keep yourself from being eaten.
For most plants, secreting toxic chemicals is a means of protecting oneself.