Fish with a swim bladder can stay at their current water depth without having to waste energy in swimming. The dorsal position of the swim bladder means the center of mass is below the center of volume, so it acts as a stabilizing agent. Also, the swim bladder is a resonating chamber, to produce or receive sound.
Swim bladders are evolutionarily closely related (i.e., homologous) to lungs. Traditional wisdom has it that the first lungs (simple sacs connected to the gut) allowed the fish to gulp air in oxygen-poor conditions. They evolved into the lungs of today's terrestrial vertebrates and some fish (lungfish, gar, bichir) and also into the swim bladders of the ray-finned fishes.