Bats, flying mammals, and those are what bats actually are. When one talks about bats, they often don't realize that they are referring to a broad classification of creatures rather than a specific type. It is like saying carnivores, which include wolves, tigers and polar bears. There are over 1000 different species of bats!
Bat species include the most common ones in North America, little insectivore bats that eat mosquitoes and other insects, weighing less that 2 ounces. The Caribbean has Fishing Bats that scoop small fish out of the water, India and Australia are home to "Flying Foxes" with wingspans exceeding seven feet! Because of this wide diversity, there is no set pattern on evolution for bats, but we can speculate as to why and how bats came about.
Millions of years ago, after the age of the dinosaurs, small mammals suddenly found there was a lot more space and food available, empty ecological niches were everywhere. Food supply also started to increase as those giant reptiles were no longer around, and it took a lot of food to sustain a 20 ton body. Mammals started to increase in numbers and branch out.
Some little mammals started climbing things and "jumping" to get food. Some developed large flaps of skin between the forelegs and hind legs that would catch the air and allow them to glide from one place to another, thus flying squirrels developed, but these weren't bats! Another group of mammals started to develop disproportionately large hands and forearms with membranes of skin between the fingers. Their bones also got thinner and lighter. By moving these hands, the mammals learned to fly and bats evolved.
Advantages of flying were numerous. There was less energy expended than climbing up and down trees. You could catch insects on the wing so had a better food supply. Also, you avoid land predators and, if you flew at night, avoided most arboreal predators as well. If you were flying at night, you needed as way to "see" and most mammalian eyes aren't that good in the dark, so sonar in bats evolved.
This evolutionary process did not probably occur just once, but several times throughout the millenniums. Many think fruit bats actually evolved from primates. They developed strange noses to better pick up the scent of flowers and ripe fruit.
How did vampire bats evolve to drink blood or fishing bats first discover how to echolocate fish? That is something we may never know!