A worm in tequila is actually a legend surrounding a particular Mexican liquor, namely mezcal not tequila. Mezcal is a spirit made from the agave plant.
Only certain mezcals contain a worm, called a "gusano", and not every bottle has one either. The worm is usually planted on the agave piña and then harvested while the piña is being prepared for distillation. There are a few different theories as to why the worm ended up in mezcal. One following legend states that the worm was traditionally eaten by locals to enhance the visions received from consuming the drink.
However, modern distillers no longer use the worm, and it is more of a marketing tactic to attract attention. The worm really has no effect on the taste of mezcal and many aficionados even emphasize that the worm in the bottle is rather distracting. High quality mezcal has no worm in the bottle and is just as delicious as the one without the worm. It is important to note that there are no worms in tequila bottles. Tequila is not made in the same way as mezcal and no unwanted animals or insects are used in its production.