The reason is simple. Baiscaly 365 (approximate number of days in a year)
is not divisible on 12 (desirable number of months=the number of Zodiac signs).
Then it was proposed to have 6 months of 30 days and 6 of 31, alternating. This still
was not good
because 6 times 30 + 6 times 31 = 366. So one month has to be 1 day shorter.
Why it was decided to make February shorter? I suppose because this was the last month of the year,
which started on March 1.
This reform was made at the time of Julius Caesar (it is called Julian calendar for this reason) and naturally one month was named after him. But he was followed by an emperor whose nick name was August, who essentially established some king of monarchy, and it was decided to name a month for him as well (for everyone to know that he was not "smaller" than Julius, but perhaps even greater).
But then it did not look good that his month is shorter than July, so one day was added
to August and subtracted of February. And the lengths of the remaining months
were changed so they alternate. (Why exactly did they have to name after him the
month which follows July, I am not sure: on my opinion, if I were charged with this,
I would simply rename September
or November in his honor (which are derived from Latin numerals 7 and 9; remember the first month was March!).
Please do not ask me for a reference: I read the story in my childhood, and do not remember the reference. Some Russian book for children, possibly Children's Encyclopedia.
EDIT. The above explanation is a very much simplified version of what actually happened.
You can get a good impression of the complexity of the question from the Wikipedia article "Julian calendar", especially the chapters "Sacrobosco's theory on month lengths" and "Names of the months". There are also many references there.
Another interesting article is "Month" in Wikipedia, the part called "Julian and Gregorean calendars". In particular, it gives the following interesting interpretation of month lengths: they correspond to the white and black piano keys! (White correspond to 31 days and F corresponds to Jaunary:-)