It’s the same “public” as in “public houses” (actually privately-owned premises) or “public companies” (publicly traded but still private interests). One more quirk of the English language.
The public in public houses presumably comes from the fact that they're open to the public to enter. There's obviously the implied need to buy something if you spend a length of time there, but compare to a private house (I.e. a residence) where the public does not by default have permission to enter.
Historically, pubs (which is short for "public house") originated as personal residences, whose owners (pub operators are still called "landlords") started creating income by making and selling food and beer to neighbours and strangers.
Public schools originally were the opposite of private tutoring, and often were genuinely targeting poor children who couldn't afford a tutor. Hence why so many of them have a charity status.
It’s the same thing in all cases: the meaning is in general “open to the general public”, often by opposition to what came before or what was the norm at the time (private schools, private salons, and unlisted companies respectively; in all cases you needed to get invited or accepted somehow and entry was restricted).
The issue is that language drifted and now “public” generally means “owned by the public” rather than merely “open to the public”.