Proper tequila can only be made from the blue agave plant found in certain areas of western mexico. The worm is an agarve grub that only feeds on that particular plant. There is a chemical in the agave plant that the grub absorbs that has hallucionagenic properties.
Eating the worm is a myth. The worm is supposed to be put under the tounge where these chemicals can be absorbed into the bloodstream therefore making you trip. A bit like a mild dose of LSD.
Of course its illegal in this country so the worms are purely for show and just soaked in tequila. I have a bottle of mezcal in my bar in edinburgh that has about 6 worms in it as no on would eat them from the last 5 bottles.