1. They act as a coach: The professor and your peers expect you to attend lectures. The assignment is due by 5pm on Wednesday. If your performance isn't satisfactory you will be dropped from the course.
2. They get better feedback about their teaching. If half the students can't do the assignment on a particular topic, the professor can schedule catch-up lessons. Watching a group of students struggle with a question can give valuable insights about how to teach that topic effectively.
3. They act as a high-quality filter: Only high-quality applicants will be admitted to the university course, while anyone can pay $10 and start doing a Coursera course. The university also offers the opportunity to become part of a valuable alumni network.
(Some online bootcamps like Lambda also have these advantages because they insist on strict online attendance and are willing to drop students who don't put in the effort)