There are molecules that sit on the outside of human cells called receptors. The receptor's job is to "sniff" molecules in the vicinity of the cell, and if it finds something it recognizes, the receptor triggers changes inside the cell — it could be to prompt the cell to make a hormone, for example, or increase the production of a particular protein.
There's a receptor on the outside of some nerve cells called TRPA1. When TRPA1 sniffs something it recognizes, it causes the nerve cell to send a signal to the brain.
One of the molecules TRPA1 recognizes is a class of chemicals called isothyocyanates — and it just so happens that foods like wasabi and mustard oil are packed with isothyocyanates. So when wasabi comes in contact with a nerve cell outfitted with a TRPA1 receptor, the nerve cell tells the brain, in essence: "Ouch."
In an evolutionary sense, the reason plants started making these compounds was to try to stop humans or other omnivores from eating them. It didn't work.