While the accepted answer does a good job of summarizing the general class roles, I would like to emphasize that once you understand the quirks of MvM, and get a group of regulars, you really can do whatever you want.
The MvM community is notorious for shunning anybody that doesn't adhere to the cookie cutter metagame. I believe this mentality flourished because of the level of difficulty and the tendency for MvM games to take a long period of time. Nobody wants to waste 2 hours and not even win, so people are pretty close minded as a result.
I have found that communication and coordination beats class selection any day. Probably the most extreme example I can think of is my buddies beating a map using only standard competitive 6v6 classes (two scouts, two soldiers, demo and medic).
That being said, there are some roles that really need to be filled no matter what.
1.) You need money. This is not ONLY a scouts job. It can be done with a competent spy or demoknight. If you don't have a scout, everybody needs to understand that they cannot just fire and forget, they need to call out money or get it themselves. Its a more involved play style, but very doable.
2.) You need heals. Scouts with milk, engineers with dispensers and medics are the main sources of healing. Milk can provide huge burst heals when applied at the right time. (If i play scout I max out my milk first, essentially turning every enemy into slow, walking healthpacks for my team). Dispensers can provide a safe zone that will let you heal slowly and get back into the fight, and the medic will allow players to hold their ground and tank. A heavy can almost solo a Giant robot if they are covered in mad milk. And once he gets upgraded, he WILL solo a giant robot if they are covered in milk. When my buddies and I play, we usually have an engineer OR a medic. Unless we are messing around and have the engineer go widowmaker, gunslinger and just try to kill everything (which works).
3.) You need to kill medics. This can be done with a demoman, sniper or spy. Demoman has the easiest time with this. HOWEVER, if the sniper can aim well enough, he will most likely outdamage your entire team and kill 75% of the wave. If he can't aim, hes simply going to be dead weight. Good snipers are few and far between, but if someone claims they can do it, I have found it is worth letting them prove themselves.
Understand that if you try to do this without friends you will most likely be kicked, due to the intolerance I talked about.
I just don't want you to fall into the mindset that there is a RIGHT way and a WRONG way to play MvM. Once you grasp what you need to accomplish to win, you can mix and match strategies and have waaaay more fun. I rarely do random lobbies anymore because you can only do Scout,Heavy,Engineer,Demo,Soldier,Pyro so many times before you want to bash your head into your keyboard.