How does natural selection explain how organisms that are poisonous evolved?
There is a lot to say on this question. I will try to keep it short, to the risk of oversimplifying the problem. I can think of three main reasons:

1. Predation does not necessarily means death of the prey

Most of predation does not directly kill the prey. Typically herbivory, will often damage the prey but a single individual won't kill the prey entirely. In such systems, being poisonous may eventually either directly stop the predator (because the predator quickly sense it is poisonous or because the predator immediately dies) or will ensure that the predator remembers not to attack this same individual.

2. Kin selection

kin selection (aka. Group selection although there are a few authors who do not equate them), is selection above the level of the population of individuals (see also unit of selection; see this recent answer for some book recommendation on the subject). 

In very short, under kin selection, an individual who dies of predation but can either cause suffering to the predator or just kill it, will protect its close relatives that live nearby from the same predator (who is iether dead or who may have learned to avoid these particular plants). As close relatives share of lot of DNA in common, a gene for toxicity - even if it fails to protect the individual being preyed upon - can protect the copies of itself present in the siblings of this individual.

3. Side effect

Let's not make the mistake of assuming that a trait has necessarily come to high frequency because it was selected for the specific purpose we associate this trait with. A plant may happen to become toxic for reasons that are unrelated to predation. While toxicity is often costly to the plant, it may sometimes eventually just be a by-product. Unfortunately, I would not be able to cite an example where toxicity is simply a side effect.