I’m not quite sure i can answer WHY Earth was formed, but i’ll have a go at HOW it was formed…..
It is believed that prior to the Earth forming there was a large ‘nebula’ – this is an interstellar cloud of dust and various gasses (hydrogen and helium). Over time, the dust, particles and gasses are thought to have ‘accreted’ i.e. been pulled together into one large mass, due to the forces of gravity – so as i mentioned in your previous question, this is about objects with mass being attracted towards one another. This process of accretion is also thought to have formed the Sun and other planets. Since Earth formed 4.5 billion years ago, various geological processes and events have resulted in the planet we see today.
Good question – and a question not really answered so far. It’s all about what happened just after the big bang, and how matter associated to form galaxies, planets, etc…And reading Katy’s answer, I just realised she explained that very well. Hope it answers your question 😉
PS: If you wonder why the Earth is spherical though – it’s because the spherical shape is the lowest energy state that a group of matter can be in. Small asteroids and moons can be non-spherical, but after they reach a certain size, all the bumps are pulled down, and they become more spherical