Well, first and foremost, it is to ensure its own continued existence. Universities are among the oldest, most stable institutions in the world; they are competitive with monolithic theocracies, water empires and parliamentary democracies in duration - and are arguably stabler to schisms (or handle them more gracefully) than the former and more enduring than the latter. They do this by being self-perpetuating oligarchies, they are self-selecting elitists; and by being independent to the extent they can while assiduously tapping social resources, both through appeal to their core population and through manipulation of social institutions (cf the Oxbridge influence on the British Civil Service, or the role of the tax code in encouraging donations to US universities). Mostly harmless too.
But, a quick order of magnitude analysis shows that at most about 1% of the output of universities are required to perpetuate themselves (at the simplest level, for example, Penn State graduates ~ 8-10,000 students per year, and to sustain ~ 2000 faculty it hires ~ 50-100 per year; fudge factors for satellite campuses and non-tenure track professional staff, we only want the order of magnitude here). So what is the point of the rest of the university members, aka students, other than having a larger pool to choose the future oligarchs from of course? [BTW - note that if you are a prospective university student who wants to maximise the probability of being a professor (WHY anyone would want this is a separate issue to be considered), then your odds are maximised, at some fixed level of high enough ability, by taking the least popular major. Er, like the physical sciences, or classics or something].
So what to do with the rest??? Well, there are the professional schools, successfully assimilated into universities. Engineers, medical doctors, lawyers, architects, etc - professions requiring university level education but whose intake at the social level is governed by independent (oligarchic) professional societies (note that the sciences are generally not professions, there are membership societies but they are voluntary associations requiring either buddy nominations, or merely a degree and a check, not independent certification of actual working competence). There's also the secondary professions, like business studies, education and psychology, which are at the same education level, but not generally governed by professional societies as such. That accounts for a fair fraction.
But, the bulk of students receive general "liberal" education. Not in the political sense, but in the sense of having studied a broad but advanced curriculum in the academic arts, having achieved some level of proficiency in some subject. Note that to some extent many useful, but advanced skill avocations are somewhat separate from this category - so nursing schools and fine arts conservatories may be associated with liberal arts universities, or not, but rather may be independent organizations or associated with technical institutions. So, the bulk of the university students are intended to study history, or literature, or a language or some general skill, that per se is not associated with a particular job in society.
So, why? And why do people pay serious money to do this, governments and charities organize huge subsidies for people to do this, and most importantly society incurs a massive opportunity cost by letting some of the most talented young people avoid work for ~ 4 years.
Well, first of all, this creates a talent pool for society; you never know when you might need a few hundred pashto speakers or experts on the history of the British conquest of the Middle East. Secondly, as a matter of experience, the discipline to study, assimilate new ideas, check facts, debate uncertainties and publicly present arguments is a valuable skill set in and of itself (one that could arguably be taught more efficiently without the time spent on Austen novels in the process, except we're never quite sure which of those skill sets are needed next). Oh, and maths. There can not be too many people with knowledge of as much mathematics as possible; people in general are shockingly innumerate, and any time spent teaching those who can take it any bit of numeracy, calculus, statistics or computational techniques is time well spent.
But, that leaves out the other two major functions of universites: one is that they are a meritocratic path to advancement, which is robust and orthogonal to the other great levelers (armed forces and mercantilism, in societies with open enough social structures). If you're good enough at something, a university will snatch you up, and try to develop you and place you (and universities have extraordinary placement networks); at its root, this is the system for self-perpetuation, but with bells and whistles to leverage the meritocracy into broader society. And universities try to make sure their own members are at least somewhat comfortable (instant metric - can the children of faculty afford to attend the university?). This is a Good, it promotes social mobility and rewards ability and hard work. It is an imperfect system of course (hey, if you know an infallible system to identify potential ability in untutored youth, we'll take it!), but every bit helps.
The other grand effect of a university education, and one actively promoted since we're aware of it,is Social Networking; both peer networking (like alumni preferences, and the somewhat unsavoury preferences given to children of well connected parents) and the critical role of taking people out of their social contexts and introducing them to new ones. Whether people later go back to their roots and bring back the experience, or they assimilate into a new social strata, the net effect is one of social mobility and homogenizing influences. And, historically, this has been liberalising and beneficial, although there are constant social pressures towards exclusivity and social segregation, both for any university as a whole (consider for example the social roles of some of the southern state universities in the US and the division between minority institutions, the landed old money institutions and the A&M colleges) and within universities (for a contemporary example, look at the spontaneous social segregation at lunchtime in most school cafetarias).
That is the other role of universities, and one they have done quite successfully. Incidentally, in the liberal democracies, the armed forces are the other institutions that have in effect served this role, by promoting to some extent on merit, mixing social classes within ranks, and geographically moving people around. The mercantile path is not intrinsically configured to serve these roles, but sometimes does so incidentally. Not coincidentally, "self-made" merchants are not infrequently contemptuous of the universities and their output...
Phew. Who knew. Oh, and sports... I don't know how US universities became training camps for professional sports, but at least this fits in with a couple of their major social roles, if somewhat imperfectly. And, having competing self-perpetuating oligarchies is awkward, better to assimilate them (anybody mention ROTC?).