Oxygen is the chief culprit. Off-tasting and -smelling compounds are formed when oxygen comes in contact with the unsaturated fats in butter. You can slow down this chemical reaction by lowering the temperature (frozen butter lasts longer then refrigerated butter, which last longer than room-temperature butter) and by tightly wrapping the butter (to maximize surface exposure to air). Another method for fighting rancidity is to change the butter into clarified butter.
Just because a butter goes rancid doesn't necessarily mean it has become unwholesome. In fact, butter's vulnerability to bacterial spoilage is lower than most people think because butter is a water-in-oil emulsion. Consequently, each water particle is sealed in an envelope of fat. This means that the bacteria dispersed in the water and in the butter cannot spread freely.