Why does the earth have layers?

The different densities of material that compose the earth’s surface create layers of like material.