Because it is cost effective to do so. There are different levels and types of fencing for military installations based on the function of the base.
Add to this the 300,000 to 400,000 anchor babies born each year to illegal aliens, most of them receiving Medicaid, food stamps, and welfare as American citizens. The fence would pay for itself many times over in just a year.