Fireflies flash to signal that they're looking for a mate.
In general, you'll see the male fireflies lighting up as they fly. The females stay put near the ground. When a female sees the flash of a male firefly, and if she is ready to mate, she'll flash back. They'll continue flashing back and forth to each other, like in a "call and response" dialog, until the male reaches the female.
Each species has a different flashing pattern, and they only respond to that pattern.
HOWEVER....some species of already mated females will then change their flash pattern to a different species pattern, luring in the male, and then...EATING it! I guess that's a way of getting sustenance without killing off their own males (such as in praying mantises, in which the females eat the males after mating!).
Not all firefly / lightning bug species flash as adults. In the United States, most of the flashing fireflies live in the east half of the country, while most of the firefly species that live in the west half don't flash. Perhaps those species find mates through pheromones, or by visual cues. But all firefly larvae, and firefly eggs bioluminesce.
The glowing and flashing of the adults, larvae, and eggs also serve to warn other animals that they taste bad.