The purpose of a foundation is to hold up and hold together the structure above it. Contrary to our everyday experience the ground is not quite still and in many cases not totally solid. A house which is just plonked down on bare earth is more likely to be cracked or damaged over time by natural forces. A properly-built foundation increases the amount of abuse a house structure can take and remain safe for the people inside it.
A building foundation actually performs a number of functions. The three most important are to bear the load of the building, anchor it against natural forces such as earthquakes, and to isolate it from ground moisture. The relative importance of these functions changes with the type of land underneath the building and the building design. For smaller accessory buildings such as sheds, a foundation is less important. The foundation often creates an underground space which can become another usable floor level, like a basement. This often has secondary functions such as storage, and is a location for "mechanicals" including furnace equipment.
In engineering terms a house foundation must bear the dead and live loads of the house superstructure built on top of it. The "dead" load is the constant weight of the house structure itself – it is "dead" because it never changes. The "live" load is the changing weights of contents and people, and in cold locations the live load includes the weight of snow on the roof. A continuous foundation can be thought of as a single structure, all four in-ground wall footings are connected together. It must be fairly rigid in order to transmit the weight of the building into the ground without being seriously compromised itself. If the ground underneath is problematic – soft soil or a wet or sloping location – additional foundation structures such as vertical piles may need to be installed to keep the structure stable.
An Anchor Against Changing Natural Forces
Once a house is built it tends to hold together like a box, especially a modern stick-built house. If it is not anchored to a foundation it can be broken apart or washed away completely by natural forces like earthquakes, wind or tornado, frost-heave, floods or tsunamis. Since a foundation is by definition built into the ground it is much less likely to be swept away. Because foundations are built out of solid dense materials like concrete or stone they resist shaking better than a house superstructure. A house which is bolted to its foundation will retain its structure better and be less likely to be swept away in a natural disaster. In many older American houses the plates and posts were simply rested on top of the foundation wall and the weight of the building was expected to keep them in place. A seismic retrofit bolts these structural members to the foundation and is a good investment in places where earthquakes, strong winds or floods are possible.
Frost heave is a less dramatic problem, but can warp the structure of a house over time. Foundation materials are less affected by freezing and so preserve the shape of the house.
Prevent Ground Moisture Rising
In areas where there is a lot of ground moisture and houses are built of wood, there is a significant problem with rot in any posts in contact with the ground. This is an issue in the Pacific Northwest and much of the north of the United States. At minimum, building codes now call for posts to be set onto concrete blocks for most structures. Due to high levels of ground water many basements will flood in the rainy seasons. A box foundation with concrete slab can be sealed to keep water out -- this protects the house from dampness as well as providing extra useful space.