Leap Year

"Concerning the solar year and types of years.
And concerning the leap year." May 29, 1564
There is a leap year every year whose number is perfectly divisible by four - except for years which are both divisible by 100 and not divisible by 400. The second part of the rule effects century years. For example; the century years 1600 and 2000 are leap years, but the century years 1700, 1800, and 1900 are not. This means that three times out of every four hundred years there are eight years between leap years.

According to the Gregorian calendar (which is in use today) the year is intended to be of the same length as the cycle of the seasons. However, the cycle of the seasons, technically known as the tropical year, is approximately 365.2422 days. Since a calendar year consists of an integral number of whole days, a calendar year cannot exactly match the tropical year. If the calendar year always consisted of 365 days, it would be short of the tropical year by about 0.2422 days every year. Over a century, the calendar and the seasons would depart by about 24 days, so that the beginning of spring in the northern hemisphere would shift from March 20 to April 13.

To synchronize the calendar and tropical years, leap days are periodically added to the calendar, forming leap years. If a leap day is added every fourth year, the average length of the calendar year is 365.25 days. This was the basis of the Julian calendar, introduced by Julius Caesar in 46 B.C. In this case the calendar year is longer than the tropical year by about 0.0078 days. Over a century this difference accumulates to a little over three quarters of a day. From the time of Julius Caesar to the sixteenth century A.D., the beginning of spring shifted from March 23 to March 11.

Pope Gregory XIII
Pope Gregory XIII
When Pope Gregory XIII instituted the Gregorian calendar in 1582, the calendar was shifted to make the beginning of spring fall on March 21 and a new system of leap days was introduced. Instead of intercalating a leap day every fourth year, 97 leap days would be introduced every 400 years, according to the rule given above. Thus, the average Gregorian calendar year is 365.2425 days in length. This agrees to within a half a minute of the length of the tropical year. It will take about 3300 years before the Gregorian calendar is as much as one day out of step with the seasons.

Source: US Naval Observatory

Systematic Error
No calendar is perfect. To make a calendar a better measure of the Earth's orbit around the Sun, leap year rules were created and have since been modified. These improvements have brought a close correlation between the calendar and the orbit, but the calendar year will always be an approximation.

Calendar Introduced Length of Year (average) Approximate Error
Gregorian 1582 AD 365.2425 days 27 seconds 1 day every 3236 years
Julian 46 BC 365.25 days 11 minutes 1 day every 128 years
365 day Ancient 365 days 6 hours/year (1 day every 4 years)
Source: Steffen Thorsen

Human Error
The leap year was introduced in the Julian calendar in 46 BC. However, around 10 BC, it was found that the priests in charge of computing the calendar had been adding leap years every three years instead of the four decreed by Caesar (Vardi 1991, p. 239). As a result of this error, no more leap years were added until 8 AD. Leap years were therefore 45 BC, 42 BC, 39 BC, 36 BC, 33 BC, 30 BC, 27 BC, 24 BC, 21 BC, 18 BC, 15 BC, 12 BC, 9 BC, 8 AD, 12 AD, and every fourth year thereafter (Tøndering), until the Gregorian calendar was introduced (resulting in skipping three out of every four centuries).

97 years out of every 400 are leap years, giving the total number of days in 400 years as
400*365+97 = 146,097
Source: Eric W. Weisstein.

Other sources:
Tøndering, C. "Frequently Asked Questions about Calendars."
Vardi, I. "The Julian Calendar." §3.5.1 in Computational Recreations in Mathematica. Redwood City, CA: Addison-Wesley, p. 44, 1991.