Your specific example is quite famous: here’s a website talking about it: https://nrich.maths.org/9601
The general theory is called the Wisdom of the Crowd, which you can explore here: https://en.m.wikipedia.org/wiki/Wisdom_of_the_crowd
It has to do with how people’s guesses lie on some bell curve, which causes their guesses to balance out in the mean or median, leading to a very accurate “overall” guess.
Now of course, this is not completely right all the time—as in the flat earth example, if you get a group of people who think exactly the same way you won’t have a good distribution. Hence, this idea from the Wikipedia:
Scott E. Page introduced the diversity prediction theorem: "The squared error of the collective prediction equals the average squared error minus the predictive diversity". Therefore, when the diversity in a group is large, the error of the crowd is small.
Interestingly, this effect of balancing different people’s guesses can be seen in one individual alone! Here’s a snippet from the Wikipedia article that extends your anecdote:
In further exploring the ways to improve the results a new technique called the "surprisingly popular" was developed by scientists at MIT's Sloan Neuroeconomics Lab in collaboration with Princeton University. For a given question, people are asked to give two responses: What they think the right answer is, and what they think popular opinion will be. The averaged difference between the two indicates the correct answer. It was found that the "surprisingly popular" algorithm reduces errors by 21.3 percent in comparison to simple majority votes, and by 24.2 percent in comparison to basic confidence-weighted votes where people express how confident they are of their answers and 22.2 percent compared to advanced confidence-weighted votes, where one only uses the answers with the highest average.