Why do we have nerves in our teeth?
Because your teeth evolved as a sensory organ.
The material teeth are made of evolved first in placoderms, it existed to sense the electrical current in the water around it, so it has to form around a nerve. in modern animals the nerve mostly functions in sensing stress and thermal stress on the tooth.
And because mammals are weird.
Teeth did not evolve to be permanent, they evolved to be lost and replaced, that is what everything that is not a mammal does. So having a nerve was not a big disadvantage (in say, reptiles) since that nerve would eventually wither and die as the tooth was lost. toothaches were not permanent they were a temporary irritation, and served do dissuade any behavior that caused them, like trying to eat food that was too hard, it was little different than the pain of thorn telling you not to grab sharp things.
Mammals are unique in retaining their teeth throughout their life so now a system shaped by evolution to be temporarily has been dragooned into something permanent. So now what would be a temporary irritation becomes a permanent thing when the tooth is damaged in mammals. Worse permanent teeth means things like stress on the teeth is even more important since you are stuck with damaged teeth so mammals actually make the nerves bigger. The addition risk of a toothache is not an issue because mammals are stuck with that risk anyway because they retain the tooth. The earliest mammals did not have long lifespan so the risk of a debilitating toothache was minimal, however we their descendants live for decades, so a structure evolved to last a year or two now has to survive for decades and it just fails sometimes. Evolution can only change what it has it cannot throw out a problematic system and start over from scratch, so it often gets stuck with such haphard thrown together solutions (kluges) And we unfortunately have to pay the price in this case