In the year 2285, Admiral James T. Kirk oversees a simulator session of Captain Spock's trainees. In the simulation, Lieutenant Saavik commands the starship USS Enterprise on a rescue mission to save the crew of the damaged ship Kobayashi Maru. When the Enterprise enters the Klingon Neutral Zone to reach the ship it is attacked by Klingon cruisers and critically damaged. The simulation is a no-win scenario designed to test the character of Starfleet officers. Later, Dr. McCoy joins Kirk on his birthday; seeing Kirk in low spirits, the doctor advises Kirk to get a new command and not grow old behind a desk.
Meanwhile, the USS Reliant is on a mission to search for a lifeless planet for testing of the Genesis Device, a technology designed to reorganize matter to create habitable worlds for colonization. Reliant officers Commander Pavel Chekov and Captain Clark Terrell beam down to the surface of a possible candidate planet, which they believe to be Ceti Alpha VI; once there, they are captured by genetically engineered tyrant Khan Noonien Singh. The Enterprise discovered Khan's ship adrift in space 15 years previously; Kirk exiled Khan and his fellow supermen from 20th-century Earth to Ceti Alpha V after they attempted to take over the Enterprise. After they were marooned, Ceti Alpha VI exploded, shifting the orbit of Ceti Alpha V and destroying its ecosystem. Khan blames Kirk for the death of his wife and plans revenge. He implants Chekov and Terrell with indigenous creatures that enter the ears of their victims and render them susceptible to mind control, and uses the officers to capture the Reliant. Learning of Genesis, Khan attacks space station Regula I where the device is being developed by Kirk's former lover, Dr. Carol Marcus, and their son, David.
The Enterprise embarks on a three-week training voyage. Kirk assumes command after the ship receives a distress call from Regula I. En route, the Enterprise is ambushed and crippled by the Reliant, leading to the deaths and injuries of many trainees. Khan hails the Enterprise and offers to spare Kirk's crew if they relinquish all material related to Genesis. Kirk stalls for time and uses the Reliant's prefix code to remotely lower its shields, allowing the Enterprise to counter-attack. Khan is forced to retreat and effect repairs, while the Enterprise limps to Regula I. Kirk, McCoy, and Saavik beam to the station and find Terrell and Chekov alive, along with slaughtered members of Marcus's team. They soon find Carol and David hiding deep inside the planetoid of Regula. Khan, having used Terrell and Chekov as spies, orders them to kill Kirk; Terrell resists the eel's influence and kills himself while Chekov collapses as the eel leaves his body. Khan then transports Genesis aboard the Reliant. Though Khan believes his foe stranded on Regula I, Kirk and Spock use a coded message to arrange a rendezvous. Kirk directs the Enterprise into the nearby Mutara Nebula; static discharges inside the nebula render shields useless and compromise targeting systems, making the Enterprise and the Reliant evenly matched. Spock notes however that Khan's tactics are two-dimensional, indicating inexperience in space combat, which Kirk then exploits to critically disable the Reliant.
Mortally wounded, Khan activates Genesis, which will reorganize all matter in the nebula, including the Enterprise. Though Kirk's crew detects the activation of Genesis and attempts to move out of range, they will not be able to escape the nebula in time due to the ship's damaged warp drive. Spock goes to the engine room to restore the warp drive. When McCoy tries to prevent Spock's entry, as exposure to the high levels of radiation would be fatal, Spock incapacitates the doctor with a Vulcan nerve pinch and performs a mind meld, telling him to "remember". Spock successfully restores power to the warp drive and the Enterprise escapes the explosion, though at the cost of his life. The explosion of Genesis causes the gas in the nebula to reform into a new planet, capable of sustaining life.
After being alerted by McCoy, Kirk arrives in the engine room and discovers Spock dying of radiation poisoning. The two share a meaningful exchange in which Spock urges Kirk not to grieve, as his decision to sacrifice his own life to save those of the ship's crew is a logical one, before succumbing to his injuries. A space burial is held in the Enterprise's torpedo room and Spock's coffin is shot into orbit around the new planet. The crew leaves to pick up the Reliant's marooned crew from Ceti Alpha V. Spock's coffin, having soft-landed, rests on the Genesis planet's surface.

Question: How was a new planet formed in the nebula?
Answer: by the explosion of Genesis