One reason that fireflies glow is to attract a mate. Males and females of the same species will flash signals back and forth as a way of communicating. Each firefly species has its own particular pattern. For example, the fireflies of one species will fly around in the night sky and dive steeply just as the flash begins and turn upward to make a distinctive J-shaped pattern of light. Female fireflies hang out on a tree branch or in the grass while the males fly around showing off their best flashes. When a female recognizes the flash from a male of the same species, she will answer with her best flash.
Another reason that fireflies glow is to avoid predators. Fireflies are filled with a nasty tasting chemical called lucibufagens, and after a predator gets a mouthful, it quickly learns to associate the firefly's glow with this bad taste! So not only does the flashing help attract a mate, but it also warns predators to stay away.
One species of firefly that can't make its own lucibufagens acquires it by eating others that can. To lure victims, these fireflies mimic the flashing pattern of another species. When the unsuspecting male approaches to find a mate, he instead becomes a tasty treat to the tricky firefly.