For a properly stored keg in a kegerator, how long the beer will remain fresh will depend on the style of beer. Pasteurized beers can stay fresh from three to six months. For non-pasteurized beers, you can expect the keg to stay fresh approximately two months.
When you open the tap/faucet, beer flows out of the keg and into your glass due to a push from the CO2. The gas then fills the space where the beer was formerly housed, and that's the “head space.” The CO2 fills the head space and maintains the pressure inside of the keg at the PSI set on your CO2 regulator.