When and where did the age of 18 become a legal threshold to adulthood? And why that age? It just hit me, its sort of an arbitrary number
I'll defer to others on differences around various wheres but can speak to the when in North America, especially the United States. It wasn't an overnight decision and it stems from a number of social changes over the 19th and 20th century, mostly related to formal education and how adults think about childhood and the various stages between birth and themselves. I'm going to borrow fairly generously from a previous question on this topic.
First, there a number of responses here and here that get at the question, but mostly from the top down. It's helpful, I think, to look at from the bottom up. In the mid-1800s, various European countries, most notably Prussia, were beginning to explore the idea of tax-funded, compulsory education for all children. Each country had their own reasons for the decision and which children would be entitled to said education but generally speaking, they were moving towards such a system with the goal of an informed citizenry. The rise of public schools in the United States provides a useful frame for how we arrived at 18.
Formal education in Early America focused primarily on the white sons of landowning men. The goal was a classical education - which meant Greek, Latin, some math, some Sciences, and the ability to memorize long passages of texts. Basically, it was education in service to an educated mind. (Their sisters would typically receive an education as well but the focus was their future as a wife and a mother. Enslaved children were rarely educated and it was often illegal to even try. Meanwhile, kidnapped Indigenous children were typically given a Christian-based education.) Slowly though, ideas around a secular, tax-funded, public education advocated by founders such as Thomas Jefferson, began to catch on.
Beginning in New England, towns and communities across the country began to build schools that had a "common" look. This meant they shifted from lackadaisical schedules (i.e. open 6 weeks, closed for 4 while the town replaces the young man who trained to be a lawyer and took the teaching job to save up some money to travel) and haphazard curriculum to a more structured ten-month schedule with breaks for holidays and summer recess and what's known as a liberal arts education (reading, science, history, math, physical education, music, art, penmanship, Greek, Latin, etc.)
To be sure, it wasn't a straight line, it wasn't uniform, and the evolution of American schools was deeply informed by institutional sexism (teaching became women's work) and white supremacy (white parents and leaders actively and purposefully kept white and Black children apart) but slowly, patterns began to emerge. One of those patterns was a structure of formal education that was, generally speaking, 8 + 4 + 4.
In 1810, the average age at Harvard was 15 1/2. By 1893, the average age of a Harvard grad was 23. In the intervening years, the American education system settled into three distinct phases - grammar school (typically 8 years in length), secondary or high school (4 years long - Freshmen, Sophomore, Junior, Senior), and 4 years of college (also Freshmen, Sophomore, Junior, Senior.)
Prior to the development of the common grammar school, young (white) children would often attend "dame" or "infant" school. In effect, a woman in the community would open her home to her neighbor's children and help them learn the basics of reading and writing through song and repetition. Dame school was an informal structure that relied heavily on women's labor above and beyond their own family responsibilities and wasn't sustainable. When they faded out, they were replaced by grammar schools and academies (early high schools), which in contrast, were formal, in a designated space, and (in theory) lead by a trained educator.
One consequence of the informal nature of dame schools was how basic bodily functions, like going to the bathroom, were treated. Generally speaking, the woman running a dame school wouldn't hesitate to help a young person use a chamber pot, go to the outhouse or privy, or clean up after an accident. However, as education outside the home became increasingly formal, it was expected a child was able to see to their own bodily needs.
Grammar schools would typically have two sessions - morning and afternoon. Because of this, few schools had outhouses as it was expected children could go a few hours without needing one and could go while they were home. Through formal and informal structures, communities settled into grammar school starting when a child was 6 or 7 years old. This usually meant they were no longer breastfeeding, could follow basic directions and control their own bodies. But not too young that formal education would break their brain as it was a common fear that too much learning too young was unsafe. (It wouldn't be until the mid-1900's that boards of education began to set strict start dates around specific months of the year and the history of Kindergarten is its own separate topic. And vocational education. Basically, everything related to education has its own history.)
Through a combination of formal policy, informal family decisions, the nature of growing from a child to a young adult and one's ability to do physical labor, and the limit of knowledge transmission, school often ended after 8 years, typically when children were 13 or 14. In large towns and cities, it became necessary further divide children as fitting all bodies into one space simply wasn't doable. Grade levels (First, Second, Third, Fourth, Fifth, Sixth, Seventh, and Eighth Grade) emerged as a way to organize. Again, this wasn't the case everywhere and it happened at different times in different places in the country but it was increasingly the norm, especially on the East and West coasts.
The "4" of high school wasn't a new invention - Boston Latin, basically a feeder school for Harvard, was founded in 1635 and by the mid-1800's had settled into an average age of 16 1/2. But HS wasn't widespread until the second half of the 1800's. By 1870, the age range at Phillips Exeter (another feeder school) was 13-19. Meanwhile, the age range at Brown University was between 18-21. Again, it was a combination of formal and informal policies and parental decisions that contributed to the sorting. Child study experts such as G. Stanley Hall established the concept of adolescence (more about that here) and preached far and wide that children in different stages needed to be kept apart from each other. He was also deeply racist, sexist down to his bones, and wildly misinformed about child development but he was very confident. One of Hall's particular areas of interest was the transformation for a child between the ages of 12 and 14. If you went to a Junior High School (which typically means a 6 + 2 + 4 +4 path), you can thank Hall for his insistence that 7th and 8th grade were a magical time and those children must be kept apart from all other children.
Another factor that influenced the grade sorting were college admission policies. In some cases, it was difficult to master all of the Latin and Greek required for admission without four years of study. Feeder schools, like Exeter, adjusted to meet those needs, which meant the grammar schools that fed Exeter had to adjust. (It worked in the inverse in other areas. Some colleges wanted to ensure they got students from particular schools so they changed their admission policies to match those schools' curriculum. This was one way colleges ensured de facto gender and race segregation.) There was also a feedback loop with textbook companies and providers. As an example, the McGuffey Readers came in seven different versions; an introductory primer followed by six, increasingly more complex readers.
Even with all of that sorting and adjusting in formal education, the concept of high school wouldn't really become the norm until the mid-1900's. The 8 years of grammar school, 4 years of high school, 4 years of college or university path was limited to a small portion of people on American soil - mostly boys, mostly white, mostly from families with means. Given they would grow up to become the men who set policy and laws, their experiences informed their policy making. So, in effect, they started school when they were 6 or 7, moved to a high school at 14 or 15, and left high school at 18 or 19. Hall and his friends pushed the message that it was deeply concerning if a child didn't move through education on the "normal" or standard schedule. This lead to changes in policy at the local level that informed retention and advancement and a subsequent tightening down of age ranges inside different school buildings or sections of a school. By the time of the National Education Association's Committee of Ten report in 1894, which would inform the thinking and conversation around high school for several decades, the 8 + 4 + 4 structure was a given. Eventually, it became the norm around the world.
All of which is to say, it slowly become the norm that a young person would exit public education in May or June of their 17th year, turning or having just turned 18 at the start of the next phase of their life - which for most young Americans meant a job or family. Ergo, adulthood.