Do corporates and governments pay universities to do their research for them? I always imagined the research is done in house? I didn't imagine Apple will pay Brown University to come up with something new?!
Some do, yes. That doesn't mean everything is done this way. But it's not unusual. I've worked on stuff that went back to Siemens Medical, Audio and BMW and some robotics manufacturers. Look at the history of h264, which was done by many companies and research institutes which in turn work with universities.
A university or research organization will not develop a ready for sale product though, they will research and develop to a specific technology readiness level (TRL), so at best a proof-of-concept. The results are handed over to those funding the research and they can use it to build future products. If it's government funded and not top-secret (usually military funded) the results are public. It's not uncommon that those who worked on it continue the work in a start-up.
A very prominent example is Penn's GRASP lab, where most projects are military founded (see:
here).
Who is doing the research in universities? The students? Who would trust that?
Staff. Professors, post-docs researchers, PhD students and a few hand-picked students.
Do universities use the funding to do research for prestige or they actually try to profit from it to make money back to the university?
Both. Researchers are driven by curiosity and what the can achieve. The university is using this for positive press/prestige. The money that comes back to the university is to cover project costs, buy new things for research (the project) and in turn, successful projects and publications means you have a higher chance for future funding.
None of the funds go to someones personal bank account, except for regular salary of those who are hired specifically for a project. Doing these projects and being successful does help with consulting jobs though, it's not uncommon to consult for companies working on related things.