If you had done well at your maktab (elementary school), your parents might decide to send you on to a madrassa (high school and college) when you were about fourteen years old. That’s the same age as most kids today when they start high school. (But Ibn Khaldun and Al-Ghazali actually both started at twelve.) Only boys could go to madrassas, and only if their parents could afford to send them. Some boys could walk to a madrassa, and some had to go to a bigger town where they had a school. Those boys slept in dormitories at the madrassa, or rented rooms nearby.
The earliest madrassas were probably built beginning about 850 AD, replacing earlier Roman and Sassanian schools. Like maktabs, madrassas were usually next to a mosque. At the madrassa, boys learned more about Islam, but they also studied non-religious subjects like algebra, astronomy, medicine, history, poetry, and law.
Madrassas were also centers for research and study for adults, like universities today. They were open to anybody who wanted to visit and learn, if the visitors knew enough to be able to understand what they heard.