How did the earth come to have a molten iron core?
Four and a half billion years ago, Earth (and the other planets in the Solar System) formed when smaller bits of matter in solar orbit came together gravitationally. As a result of those multiple collisions the kinetic energy of motion was converted to heat energy, resulting in a hot, molten mass that would become our Earth.
While in this hot, fluid state, the denser, heavier material (iron and nickel) settled to the center and the less dense rocky material floated nearer the surface, resulting in today's iron/nickel core.
As the Earth radiated energy into space, the outer layers cooled first. The core, being insulated by Earth's outer layers, took longer to cool. That is not the complete story, however. In addition to the initial energy of formation, there are radioactive elements in the Earth with long half lives that continue to release energy. This energy cannot immediately escape into space either, and also accounts for the Earth's hot, molten interior.
Paul Walorski, B.A., Part-time Physics/Astronomy Instructor