Although it's a common misconception, tequila never contains a worm in its bottle, according to The Straight Dope. Mescal, tequila's cheaper relative, sometimes contains a worm thanks to Jacobo Lozano Paez, a Mexican entrepreneur who first implemented the worm as a marketing gimmick in 1950.
Both tequila and mescal are made from the agave plant, which often contains the agave worm. Actually a butterfly larva, this worm burrows deep into the heart of the agave plant. Worms would occasionally wind up cooked into the mescal mixture itself, and Paez, an enterprising businessman, deemed the worm a vital part of the beverage's flavor. He began selling mescal with worms included on purpose, and a tradition was born.