We know it's hereditary because Anakin fathered Luke and Leia, both force sensitive. Leia and Han then had three kids, all force sensitive. Clearly it's a dominant trait, since both Anakin and Leia had non-force sensitive partners, and all of their children are force sensitive.
Episodes I, II, and III made it clear that force sensitivity is not exclusive to humans - there are quite a few non-human species which are force aware.
While we can't state definitively when force sensitivity first occurred, it has definitely been around for thousands of years.
When I first saw Episode I, I dismissed the concept of Midichlorians as nonsense. However, the more I think about it, the better it seems to fit Star Wars canon. As a symbiotic micro organism, midichlorians could easily be passed from parent to child. Similarly, they could easily exist amongst multiple species1.
This explains how multiple species share a similar, seemingly hereditary trait, but still doesn't explain why there are so few force sensitive individuals.
The midichlorian count referenced in Episode I is on a per-cell basis. This implies that midichlorians can replicate (either as part of cellular reproduction or separately).
If they're able to replicate freely, and provide significant advantages to those with high midichlorian counts, there must be some reason that high midichlorian counts are uncommon.
Force sensitivity doesn't seem to have a negative effect on reproductive capability (Luke and Leia both have children). It also doesn't seem to have any adverse effect on health or lifespan (the opposite, actually).
Wookiepedia states that midichlorians are sentient beings. Based on the evidence, I'm forced to go one step further:
Midichlorians are sentient beings engaged in communal, intergalactic communication and cooperation, intentionally governing and limiting their concentration in certain individuals.
I can only guess as to what their objectives might be, but it's reasonable to assume that in addition to facilitating communication between force aware individuals through The Force, midichlorians themselves are also communicating through The Force.
1. Wookiepedia claims that they're isomorphic, implying that different species gained midichlorians through convergent evolution. This seems reasonable, but doesn't necessarily mean that the transmission of midichlorians is strictly governed by a genetic mechanism.