agree with this
for a lot of people (women in particular) gyms are (or feel) safer than walking alone at dark
they can also be less intimidating - most people just get on with their own thing at the gym - nobody calling you names or beeping their horn or worse
if you get injured there will be someone to help straightaway rather than falling over somewhere isolated and being stuck for hours
more variety at the gym - classes, weights, cardio etc.
classes can be a lot more fun. If you don't live somewhere with lots of 'nice' walks easily accessible then walking around your housing estate or muddy local park for the 1000th time can be very boring
most people walk alone or with someone they already know. Yes, sometimes you exchange a 'morning' with other people (particularly if you have a dog), whereas classes can be a lot more interactive and sociable - lots of people make friends at the gym. So if you, for example, live alone and WFH then doing a fun class would probably benefit you and 'be better' for you more - both exercise and social interaction
no chance of getting suddenly soaked in the gym
you can wear the same clothes all year round in the gym - no need for waterproof coats, boots, etc
if it's a nicer gym you can also swim, go to the sauna, etc at the same place
treadmills are usually easier on the joints than paths so good for people starting out exercising/overweight/with arthritis etc
All things being equal, would I prefer to walk three miles on the beach on a sunny day or on a treadmill in the gym. Beach, definitely. But it's quite arrogant to say that an outside walk is always "better", ignoring personal circumstances.