Tur (Orach Chaim 427) explains as follows:
A lunar month is 29-1/2 days plus some extra chalakim. Since a month has to have an integral number of days, the months (usually) alternate between 30 and 29. The 30th day, then, is always going to have to be Rosh Chodesh:
If the month was 29 days, then the 30th day is the first day of the next month, and is Rosh Chodesh by definition.
If the month is 30 days, then part of the 30th day really already belongs to the next month (because, as above, the month should really have ended midway through that day), and therefore that day is celebrated as Rosh Chodesh.
In the latter case, then, you're going to have two days of Rosh Chodesh, since after all the 30th day belongs to the previous month, and so the first day of the next month is again Rosh Chodesh by definition.
I disagree with the answer above. The month is not decided by the moon cycles, but by Witness testimony, so any astro science becomes somewhat irrelevant. For example if there are witnesses accepted when the moon is not new yet, we go after the Witnesses, not the actual placement of the moon. So I thinks its wrong to say: then part of the 30th day really already belongs to the next month
2 days comes from the same problem that creates 2 days Rosh HaShanna in Israel (although RH in Israel is fundamentally different as the 2nd day of the month is celebrated as a festival, which could never happen.) You will see that 2 days Rosh Codesh is only when the previous month is 30 days. If it is 29 days, the rosh chodesh is only 1 day.
Since a month has to be 29 or 30 - As explained in the previous answer the 30th day is either the 30th day of month A, or day 1 of month B. Day 30 is the only unknown in all this. Witnesses will either come on the 30th and turn it into the 1st, or they wont come, and the 31st will have to become 1st of month B.
Witnesses have to come in the daytime, so even if they come at the earliest time possible, the 30th has already had 12 hours before it turns into the 1st.
So we keep every 30th as a Rosh Chodesh in case the Witnesses come.
According to what I am saying, if the Mashicah comes on the 30th of the month, and the court is established in time, and witnesses come, they can technically turn that day into the 1st. So we have to keep it just in case.
(Similarly by Rosh hashanna - its not a delay in witnesses but a fundamental problem that what day it is today will not be know till some point in the future that creates the 2 days in Israel. And we now keep the 2nd day on day 2(not 30 & 1) as a remembrance of this)
I wondered about this for many years, until I finally found that the (13th century Italian) Shiboley HaLeket discusses the question explicitly. See the Shiboley HaLeket in Siman 168, who in turn quotes the (fellow 13th century Italian rabbi) Rabeinu Yeshaya which can be found in Rabeinu Yeshaya's Teshuvos, number 32.
Loosely translated and highly paraphrased, what Rabeinu Yeshaya says is that the reason Rosh Chodesh is two days only in cases of 29 day months is because there are two days that each have aspects of being Rosh Chodesh. Day "30" becomes day 1 of the incoming month, and hence is the day of actual kidush hachodesh. Day "31" which is now officially day 2 of the incoming month, is considered Day 1 for matters of counting the month and for legal documentation(שטרות). Since both days have aspects of being the first of the month in that situation, we observe two days.
A little bit of background for this. In the days of the Temple, the new moon was actually CORROBORATED by witnesses. They were not the ones who declared whether a new moon was sighted or not. That task fell to the court that sat at the Temple, whose task it was to ensure that the witnesseses actually saw a new moon in its proper place. However, in some exceptional cirucumstances, a court can proclaim unilaterally that a month has an extra day in it or not. There is a story of Rabbi Akiva proclaiming that an extra day should be added to a month - this, while he was locked up in prison (Yevamot 104).
Having said all that, in our day the Jewish calendar was set in its way by Hillel II. Realizing that being able to proclaim a new moon by a court of Rabbis given proper semicha (not the kind given nowadays, but given from teacher to student from Moses to the time of the second Temple) would soon be impossible, he and a court unilaterally proclaimed the new moon for each month based on calculations (including ensuring that holidays such as Yom Kippur would not fall on a Friday or Sunday). In this case, some months have 29 days and some have 30 days. Even though we can nowadays compute exactly when the new moon is, we still use the calendar based on the idea that only a Jewish court of equal or greater knowledge can overrule a ruling from a previous Jewish court. Once we get to the time of the Messiah, then we will go back to having witnesses telling us when the moon was visible or not.