Why do the planets orbit the sun in an elliptical shape?

The planets orbit the sun in an elliptical shape because of the combined influence of gravity and their own momentum.

According to Kepler's laws of planetary motion, planets move in elliptical orbits with the sun at one of the foci. Gravitational attraction between the sun and the planets causes them to constantly accelerate towards the sun. However, the planets also possess a forward momentum or velocity due to their initial conditions during the formation of the solar system.

The interaction between the gravitational pull and the initial forward momentum leads to the planets' elliptical orbits. If the gravitational pull were the only force acting on the planets, they would follow a straight path towards the sun. But the forward momentum causes the planets to continuously move forward as they are pulled towards the sun, which results in an elliptical trajectory around the sun, rather than a straight line.