To a considerable extent, this is for historical reasons, because of the way the sport evolved. I'll talk about the US, which is what I'm familiar with. In the US, roped climbing was introduced ca. 1930. The standard at this time was that you tied the rope around your waist (there were no harnesses), people climbed in boots or tennis shoes, ropes were made of hemp, pro consisted of pitons and natural pro, and the standard belay was either a hand belay or a hip belay, often just from a stance rather than an anchor. Pitons were initially only in thin sizes. There were no belay devices, and the Munter was not used for belaying.
Given this technology, it was extremely dangerous to take a fall. A rope couldn't be trusted not to break when there was a hard lead fall. There was a saying, "The leader must not fall." If you could get a piton in to protect a difficult move, that was great, but the expectation might be that you would spend a whole day climbing a multipitch route and place three pitons. At a place like Tahquitz (the birthplace of the YDS), people would sometimes place a piton in a certain belay and intentionally not remove it, for the safety and convenience of other climbers. Other than that, a belay would often just be a belay where you stood on a ledge and braced yourself.
So because there were so many other weak points in the safety systems, there was no realistic expectation that all falls could be protected against, and to some extent that remains true today for trad climbing and mountaineering. For example, it's often impossible to protect against falling and hitting a ledge.
At some point people started placing a small number of bolts on certain routes at Yosemite, Tahquitz, and Joshua Tree. For example, if there was a slab pitch that couldn't be protected with pitons (or, later, chocks), someone might put in one bolt half-way up it (example). The ethic was that this should not be overdone, and all bolts should be placed from a stance, not hanging on the rope. So even when people did start placing a series of bolts on a slab climb (example), they would be pretty far apart, because there just weren't enough stances.
Sport climbing was a later evolution of the sport, and was initially very controversial. The defining characteristic of sport climbing is that people bolt the routes on rappel rather than from a stance. This makes it possible to place lots and lots of bolts, which means that people can attempt harder climbs without unacceptable risk.
Bolts in the gym are placed even closer together than for outdoor sport climbs. There's no reason not to do so, since the walls are uniform and you can have a bolt hole anywhere you want. Gyms need to pay for insurance, and inevitably they will get some climbers who are insufficiently experienced and mess something up. To maximize safety for those people, it helps to place the bolts pretty close together. However, this additional safety is in large part imaginary. Until you make about the third clip in the gym, there's considerable risk of falling to the mat regardless, especially if you clip at your shoulders rather than at your hips. After you make the third clip, the close spacing of the bolts doesn't really help with safety. It just helps psychologically and makes it so the fall factor that your belayer has to catch is lower, which is easier for them.