Why was the earth formed in layers?
Since the early formation stage of the Earth, it was pull towards center that made pressure, temperature and density to increase towards the center. This is the primary reason.
The density varies from top-level 2.2 gm/cc to 13.1. gm/cc, nearly,
near the center.
While the average temperature at the top is about
comparable to the surface temperature of the Sun.
Pressure increases from 0 ( + atmospheric pressure from above) to
about 350 mega pascals at the center..
It is now evident that, in the process of formation of the Earth, over a
few billions of years, relocation by lateral and vertical movements
for stability resulted in the formation of nearly spherical shells of
layers of compatible materials that could bind together.
There is no perfect surface between any two layers. Yet,
classifications are made using characteristics, including viscosity.
A broad classification is
Crust-Mantle-Outer Core-Inner Core..