The ancient Romans had twelve months on their calendar and they alternated between having 31 days and 30 days. Although the ancient names were not identical to ours, many were similar.
Those we now call January, March, May, July, September, and November all had 31 days and both February and August were among the others, which had 30 days.
July had been named after the emperor, Julius Caesar.
When Augustus became emperor he wanted a summer month named after himself, but didn't want his month to be shorter than the one named after his predecessor.
He had to be careful which month he claimed as his own and which one the extra day was taken from, because he believed if a month named after a powerful god were changed that god might become angry and punish him. For that reason one day was taken from February and added to the month we still call August, after Caesar Augustus.
Much later astronomers discovered there are not exactly 365 days in a year so, in order to keep the calendar in line with the seasons and phases of the moon, an additional day is taken from February except in leap years.