Great question! We can never know for certain (unless we invent time travel!) what selection pressures resulted in bats evolving powered flight, but we can make an educated guess! Animals don’t spontaneously evolve flight, it takes a very long time and involves many intermediate stages. Bat’s probably evolved from animals that lived in trees, perhaps something like a small shrew. Random genetic mutations that resulted in webbed feet and the gradual lengthening of finger bones over evolutionary time likely enabled bat’s ancestors to glide from tree to tree, and this may have provided an important advantage by enabling the animal to better avoid predators or more easily reach new trees with better food resources or roost sites.
Being highly mobile is a big advantage to animals and so Darwinian selection for powered flight is likely to have been strong. Indeed, once bat’s ancestors developed flight, there was an explosion in the number of early bat species because they entered an ecological niche (the night sky) that few animals besides insects occupied before (i.e. they had little competition from other non-bat species), and so bats diversified quickly once they evolved flight. Now there are over 1300 species of bats in the world, making them the second most successful mammalian group after rodents.
Evolving powered flight appears to be quite difficult though as we know from genetic studies that all modern day bat species evolved from a single flying ancestor. If evolving flight was easy, we would expect it to have occurred in many different groups of bats separately.