The year is defined as the interval between two vernal equinoxes. The vernal equinox is the instant when the Sun is above the Earth's Equator while going from the south to the north. (Of course, it's actually the Earth moving around the Sun but it's easier to say that the Sun is moving.) The vernal equinox is also the beginning of the astronimical spring. The interval between two vernal equinoxes is 365,24219 days. This definition of the year is called the tropical year. Because its length doesn't correspond with a whole number of days, there should be some type of variation in the lengths of years.
Julius Caesar introduced the leap day in 46 BC. In his Julian calendar, an average year consisted of 365,25 days: three years had 365 days and one 366. This decreased the difference between the calendar year and the solar year, but there remained a slight discrepancy.
In 1582, the discrepancy between tropical and calendar year had grown to 10 days. Popew Gregorius XIII therefore chose 10 days in which no important holy days were celebrated. He eliminated these 10 days, letting October 4, 1582 be followed by October 15. He also changed the simple Julian rule for leap years and brought into force the current rule: every four years there's a leap year, but century years aren't leap years except when they're divisible by 400. Catholic countries adopted the Gregorian calendar immediately, but protestants introduced it more than a century later - in 1700. In 1753 Britain and its colonies adopted the Gregorian calender and skipped 3rd till 13th September.
Still, the Gregorian calendar ain't perfect. Each year, we fall behind approximately 26,3 seconds. As a result, in the year 4909 the calendar will be a whole day behind on the tropica year. Therefore, it's proposed that the year 4000 won't be a leap year.