The actual lunar month is 29.5 days. So if you religiously stuck to that, you would alternate months of 29 and 30 days. But at the end of the solar year, you would have about a third of a month left over.
The Republican Romans used a lunar calendar, so they would add an extra month every so often to get back into line. As a government official did this, sometimes a year would be left short or extended for political purposes. In one period the official was off in Spain, and nothing was done for a long time and the months drifted quite a bit.
The Egyptians tied themselves to the sun. The extra days were shoved into the months more or less equally, giving months with 30 and 31 days. But then they discovered that there was an excess quarter day in the year. The Egyptians said "to heck with it" and let the start of the year drift. This was called the Sothic Cycle.
Julius Caesar the dictator decided to reform the Roman calendar and adopted the Egyptian solution, but he added an extra day every 4 years to fix the Sothic drift. But the Romans thought that February was unlucky, so they made it shorter (28 days) and added more days to other months. The Leap Day was added to February.
In the Middle Ages, Pope Gregory made a subtle change to the Julian calendar, dropping 3 leap days per 400 years, as it had drifted somewhat over the intervening 1500 years.
A Bunch of Stuff on February in Rome
from the Web
Februrary (at the end of the year)
Apparently Februarius, when adopted, had but 23 days – traditionally the 23rd day of that month was the end of the calendar year. That indicates Februarius was observed in pre-Romulan times when months had as few as twenty days. Also, adding five days at year-end (to extend February’s length to 28) is similar to the change made by many other peoples who, around the time of Rome’s founding, added five days to their own calendar, but considered them to be unlucky and not part of the normal year.
Why is our leap day in Februrary, not the end of the year?
Romans always reconciled differences between calendar and solar year lengths during the "Month of Purification." Whenever and however Roman calendars were modified to correspond to year length, it was always done after the 23rd day of February, traditionally the last day of the year. Even in our time, leap year is observed with a 29-day February. To purists, "leap day" is February 24, not the 29th.
Plutarch wrote: "Numa...added an intercalary month, to follow February, consisting of twenty-two days, and called by the Romans the month Mercedinus. This amendment, however, itself, in course of time, came to need other amendments." (When observed, that leap month always immediately followed February 23.)
According to historian Livy, Numa divided the year into twelve months, corresponding to the moon’s revolutions. But as the moon does not complete thirty days in each month, and so there are fewer days in the lunar year than in that measured by the course of the sun, he interpolated intercalary months and so arranged them that every twentieth year the days should coincide with the same position of the sun as when they started, the whole twenty years being thus complete. He also established a distinction between the days on which legal business could be transacted and those on which it could not, because it would sometimes be advisable that there should be no business transacted with the people.
Others claim that it wasn’t until 452 B.C.E. that a month named Intercalaris was added to the Roman calendar in order to add those days required to bring calendar length back into phase with the solar year. This month also began after the 23rd day of Februarius. It was observed every second year and was said to have had a length of either 22 or 23 days, with the remaining five days of Februarius added after them.
So to sum up - February was always short in Rome, it was not reduced for Augustus, and adding 5 days to it was considered unlucky.