The first schools were all private schools. Your parents had to pay for you to go there, the way your parents pay for music lessons or dance lessons today. There were schools like that in ancient Egypt and in West Asia, in India and China, by around 3000 BC, and maybe earlier.
But as state governments formed, they began to see that they needed a steady supply of educated people. They needed people to keep their accounts and records, collect taxes, and judge law cases. So they started free schools where a few children could learn to readand write and do arithmetic. Then they could work for the government when they grew up. But most kids still didn’t go to school, unless their parents could afford to pay for it.
In the 500s BC, as the first empires formed, and the Silk Road made countries richer, the first big religions formed too. Both the empires and the religions started their own schools and universities. Buddhist monks started a university at Nalanda, in western India, and another at Taxila, in what is now Pakistan. In Athens, which was also very rich thanks to its colonies and slaves, Plato opened his Academy and Aristotle opened the Lyceum,. Both of these were private schools you had to pay for, but by the 300s BC, the great University in Alexandria, in Egypt, was free.
Beginning in the Han Dynasty, the Chinese emperors started a lot of free schools – mostly for boys, but some for girls too. The Roman Empire didn’t run free schools, but it did open state universities and pay the professors. By 500 AD, the Chinese government saw competition in the form of Buddhist monastery schools, and soon after that Christian monasteries also opened schools in Europe and West Asia.
In the Middle Ages, the Islamic Empire was the big center of education. Religion and government were pretty much the same, so together they ran elementary schools (maktabs) and universities (madrassas). The big Islamic universities in Africa at Timbuktu, Fez, and Cairo, and also at Baghdad (Iraq) and Cordoba(Spain) were the main Early Medieval centers of learning.
But as Europe got richer in the 1200s, and India, West Asia and Africa got poorer, the Catholic Church began to run big universities in Europe, at Paris (1200), Naples (1224), Cambridge (1209) and Oxford (1096), and Krakow in Poland (1364). Jewish teachers like Rashi started their own yeshivas.
With the end of the Silk Road in the 1500s, poorer governments fell further behind Europe on education. Europe was getting a lot of money from enslaving people in the Americas and Africa, and from controlling world trade. The Ottoman sultans and the Russian czars both opened state universities, but they couldn’t really afford to keep them going.
You need a lot of money to support good schools, and then places with good schools have more educated men and women who make a lot of money, and their schools get better and better. On the other hand, poor places with poor schools tend to get poorer and poorer. Even though education is very expensive, it helps countries get richer.
By the 1800s, a lot of poorer governments like the Safavid Empire were sending smart kids to Europe or America to get an education. They hoped these kids would come home again and help their countries, but a lot of them chose to stay in America or Europe – and that helped America and Europe get ahead even more.