Luck, and the location of the farm, relatively far from major towns and cities.  Walkers are attracted to people and noise, and the farm had little of either.  There isn't much explicit canon information to explain this, but I read the comics and watch the show obsessively, I've read some of the novels, and I'm a zombie fanatic.  

The farm wasn't spared completely though - many of Herschel's family, friends, and neighbors were in the barn as walkers, because they were bitten.  There are zombies in the area, but not as many as in other places.