Why do American colleges and universities have sports teams? In the USA, college sports are popular, and colleges may offer scholarship based on athletic skills.  Yet, universities spend significant money on sports, and nobody earns as well as the head of the sports team.

Considering that the universities are losing money on it, and it's not their core task, then why do they spend big money on sports?  Who benefits, and how?  Do all major universities have commercialised sports teams, or are there major exceptions of universities choosing not to take part?
Here is one side effect of a university having a famous sports team as mentioned by Federico Poloni in a comment: people know your name. This helps recruit new students, it helps alumni impress potential employers with a degree from somewhere they have heard of! I only know that Boise State University is actually a real university (and as it turns out a pretty good one) because their football field has blue turf.  

One feature of American colleges and universities that is easy to forget is that they are often in the middle of nowhere. Pennsylvania State University is in a town named State College. You can guess which came first. So imagine you have thousands of young men and women in a place that is barely a town. What do they do on Saturday afternoon? Some will start organizing teams to play sports and then start going to nearby schools to play their teams. This grew greatly since the old days but the idea that a residential university is partly responsible for providing non-academic activities for their students take part in still exists as a real force. At smaller schools which do not have sports scholarships the sports teams are more about playing because the students enjoy it and it is just part of campus life. 

Also at many schools the mission statements include character formation such as "building leadership skills." If this is the case you can actually argue that having some level of athletic competition on campus actually is part of the core mission. Maybe not an absolute vital part but one that contributes to the mission. 

I am of course ignoring in large part the money and corruption that is part of the NCAA Division I level of college athletics. Of which there is an extraordinary amount of both.

Most schools, except for d3 schools, break even with their athletic programs. Americans want to be proud of something, that something for colleges is athletics. Most people wouldn't want to go to Harvard if they didn't have a good football team. I helps to bring diversity (age, interests, grades, and money) into colleges.