Universities can presumably fund new housing, after all they are benefitting from the students being there.
Admittedly this is going back over half a century, the vast majority of first years lived on campus at my uni (I didn't and it made a hell of a difference in how I settled in - in a negative way). The university benefiited, students stayed on campus, students spent on campus. People on the same course, even if not in the same building, may be close by. I notice that there is now a Student Accommodation Code (for university managed housing). Assuming it has teeth, that must be a good thing all round; for the university, it may lead to one less reason for a student to drop out, The trouble with some of the external providers is that they will just want to fill the rooms - taking some students from institution A, or B , or C, not caring whether the students have anything in common or not.
Of course, this is going to vary, one university to another, newer ones tended to be more out of town, with maybe more space, for older ones the town or city may have partly built up around the university.