What causes the rear hanger to snap off? I have a Specialized Allez Elite road bike, purchased brand new in June this year. So far I've covered around 300 miles in total on it.

During my ride yesterday, as I was going up a slight hill, I switched to the smaller front chain ring. As I did this, I heard a sickening crunch, pedals jammed, looked down to find one of the gear changing parts is jammed into the back chain rings and chain has come off. Luckily I managed to get onto the footpath and dismount without any problems.

I don't know much about bikes so have provided an image of the damage:


Can anyone advise on what parts will need replacing? Also, how can I prevent this from happening again?

Thanks
The derailleur is not a particularly strong part, and in fact when it/its hanger breaks it can often save your frame. 

Some common reasons for it breaking are:


Hitting an obstacle (e.g. tree branch)
Going into the spokes (badly set low limit screw or the derailleur was wobbling enough for some reason to get caught in the spokes)
Chain too short.


What you need to do is buy a new compatible derailleur [e.g. whatever was spec'd on it before] (and possibly a derailleur hanger as well). Then you need to get the hanger alignment checked out (using something like the Park Tool DAG-2). Then, get the spokes checked to see if they have taken any damage and have adequate tension (you will likely want to ask a bike shop about this). 

It looks like the protector disc (plastic) is still in tact without any damage.