For example, the engineer doesn't have the most health, armor, or the most powerful weapons, but he has powerful emp grenades which can kill several enemies at once and can also build armor and ammo dispensers and sentry guns which will shoot at enemies when they enter the room. So, he is ideal for defense. Random picks a class for you at random, and changes when you die. HWguys are big, slow, heavily armored, and carry powerful chainguns and grenades. Demomen can use detpacks to open a secret tunnel entrance to the enemy fort.
Medics can heal team members and poison the enemies.
My advice for new players is to test all the classes until you get a feel for each, and find which ones you like the best and how to take advantages of the strengths of that class. You may find yourself to be a good spy, so you take advantage of being able to disguise yourself as a member of the opposite team, and use this to stab your enemies in the back. Due to all the player options, you can do so much experimentation, which aids in the prevention of getting sick of this game. I was still learning new tricks weeks after I started playing. Teamwork is a key.