There's often no mystery about why it happens. It could be that you stubbed your toe or dropped something heavy on it.
Sometimes, though, you can get tiny breaks in the bone from the regular impact of activities such as running and basketball. This is called a stress fracture.