The layers you mention are called 'Stratigraphy' and they are layers of materials soil, rock, human/animal remains etc that have built up over time.
You ask why history is underground - this is due to weathering and erosion which is the displacement of soil, mud, rock by natural forces such as wind, rain, ice, gravity and living organisms (bio-erosion). This free flowing material will naturally settle in areas and build up over time. Plus settlements and areas of human habitation would naturally increase the material depositation for reasons of fire building, shelters/structures, human waste and other food/material waste. Even in early pre-history they would produce alot of waste which they would often tip into trenchs etc which would add to the overall number and thickness of layers - stratigraphy. Areas chosen as a settlement site would have ease of access to raw building materials, food and water and for that reason old settlements will be reused time after time adding to the history to result in stratigraphy which is older the lower down you dig - the deepest remains you find were the first people to live on that site.