There are a couple of reasons. First, it’s a hygiene matter. Typically, people tend to only wear a swimming suit while at the pool, not for other activities. Gym shorts or bathing suits with underwear worn underneath is more likely to be unsanitary or soiled and potentially introduce bacteria and germs we try to keep out of the pool/ While chlorine is used to disinfect the water, the extreme amounts required to kill germs such as cryptosporidium, shigella or giardia, would be an unsafe amount of chlorine for swimmers.
The second reason is a safety concern. To avoid becoming heavy when wet, bathing suits are lightweight and do not absorb very much water, making it easier for a person to swim. Generally, a swimming suit is tight fitting and will not impair vision or breathing unlike t-shirts or cover ups. Additionally if a lifeguard attempts a rescue, clothing other than a bathing suit can hamper the rescue and endanger the individual and lifeguard.