My Pedal Fell Off My Bike!

I was recently in an accident and (mildly) injured because my bike pedal fell off. Why did it happen? And how can you prevent it from happening to you?