Elements heavier than iron are formed when a dying star explodes, specifically during a supernova event. This process allows for the synthesis of heavy elements through nucleosynthesis mechanisms such as rapid neutron capture (the r-process). So the correct response is:
as a dying star explodes.