it is genetic - the bone bone that goes from the back of the big toe to the ankle is longer than it should be. This realignment of the joint location changes the distribution of weight to this joint where the bunion grows. The weight is increased, causing the bone to grow large into a deformity. If shoes are not designed properly, this condition can be exacerbated, causing the bunion to grow larger quicker.
This is my understanding of what my surgeon explained to me.
I'm not sure if there is a fool proof way to prevent them. My surgeon noted that there is something one can put on the toe at night to push it back in alignment, but with really severe deformities, this may not help much. Avoiding shoes with high heels probably helps, but I'm guessing. My grandmother and both of her sisters had this condition, back before the days of high heeled shoes (they were born around 1880) - or at least I don't they wore them. They had to cut holes in the sides of their shoes to relieve the pain from their bunions.
Hope that helps