It was a school that was open, in principle, to anyone. Think ‘free as in speech’ vs ‘free as in beer’, but with the added sense - like ‘public transport’ - of being democratic and round-about-accessible to all.
As for the US: when it began, fee-paying schools were the dominant mode, and there wasn’t really an aristocracy with private tutors to distinguish it from. So it never needed the ‘public’ - and when government schooling became a thing, it pretty naturally took on the ‘private’ qualifier instead.
https://www.merriam-webster.com/dictionary/public%20school
“An endowed secondary boarding school in Great Britain offering a classical curriculum and preparation for the universities or public service.”
The purpose of public schools isn’t academic excellence (though of-course they support it for their most able pupils) but networking and the ‘habits of person’ necessary to compete socially in class-sensitive institutions.
The state did not run schools when these were established. It's much older than that as a term.