Apocalyptic thought has a tradition that dates to the Persian prophet Zoroaster in the 14th century BC. Recently, anxiety has grown over the prediction of the end of the world in the Mayan calendar.

It's true that the Mayan odometer will hit zeros on 21 December 2012, as it reaches the end of a 394-year cycle called a baktun. But this baktun is part of a larger 8,000-year cycle called a pictun, and there's no evidence that anything astronomically untoward will happen as the current baktun slides into the next. However, that hasn't stopped the feverish speculating that sells books and cinema tickets.

What kind of catastrophe would it take to end the world? Astronomical intruders provide a potentially serious threat. Impacts can be caused by stray rubble from the Asteroid Belt and the rocky snowballs that travel in highly elliptical orbits in the comet cloud. There are many fewer large bits of debris than small bits, so the interval between large impacts is much longer than the interval between small impacts.

That's good news. Every century or so, a 10-meter meteor slams into the Earth with the force of a small nuclear device. Tunguska was the site of the last, in 1908, and it was pure luck that that meteor landed in the uninhabited wilderness of Siberia. Every few thousand years, Earth can pass through unusually thick parts of the debris trail of comets, turning the familiar light show of a meteor shower into a deadly firestorm. Roughly every 100,000 years, a projectile hundreds of meters across unleashes power equal to the world's nuclear arsenals. The result is devastation over an area the size of England, global tidal waves (if the impact is in the ocean), and enough dust flung into the atmosphere to dim the Sun and kill off vegetation. That could ruin your day.

Then there's the "Big One". About every 100 million years, a rock the size of a small asteroid slams into the Earth, causing global earthquakes, kilometre-high tidal waves, and immediately killing all large land animals. Creatures in the sea soon follow, as trillions of tons of vaporised rock cause drastic cooling and the destruction of the food chain based on photosynthesis. There's good evidence that this happened 65 million years ago and our tiny mammal ancestors were the beneficiaries as the giant lizards were extinguished.

A hundred million years sounds like a safe buffer, but the next one could happen at any time. But you can take it off your worry list – astronomers have it covered. A network of ground-based telescopes scans the skies for bits of rogue rubble larger than a few hundred meters. That's ample time to dust off the nuclear arsenals for an interception mission if we had to. Unfortunately, the Dr Strangelove approach creates lethal shrapnel travelling in the same direction as the original object; a smarter strategy is to send a spacecraft alongside it and gently "tug" it with gravity onto a slightly different trajectory.

When massive stars exhaust their nuclear fuel, the result is a titanic explosion called a supernova. The dying star brightens to rival an entire galaxy and emits high-energy particles that can destroy the ozone layer of a planet like Earth if it occurs within 30 light years. The demise of large North American mammals 41,000 years ago has been linked to a supernova, and several other mini-extinctions may be tied to the cataclysm of stellar death.

A supernova is a small squib compared to a hypernova. In this dramatic and rare event, the violent collapse of a very massive star ejects jets of gas and high-energy particles at close to the speed of light, and for a few moments the star outshines the entire universe in gamma rays.

If a hypernova went off within 1,000 light years, and Earth was within the narrow cone of high- energy radiation, we'd experience an immediate global conflagration. It's brutal luck if a hyper nova ever goes off with its beam aimed at us.

On longer time scales, attention turns to the sheltering Sun. Our constant companion is midway through its conversion of hydrogen into helium. In about 5 billion years, its guttering flame will be extinguished. The Sun's diffuse envelope will engulf the Earth and turn it into a lifeless cinder. This is death by stellar cremation.

If that seems like a comfortably distant prospect, the biosphere will actually die much sooner. The Sun burns hot as it gets older, and in 500 million years a turbocharged version of global warming will turn the Earth into a global desert.

That gives us plenty of time to find better real estate. Titan looks promising. It already has the nitrogen – just add oxygen and presto! Our second home. And those wild-eyed rocket scientists who want to save us from asteroids have a thrilling plan up their sleeves: deliberately bring an asteroid in close, and with each pass it will transfer a little energy to the Earth and nudge it further from the Sun. After a few million close calls we'll have migrated to a more hospitable orbit.

*****

Stars come and go but galaxies seem eternal. A galaxy like the Milky Way acts like it has all the time in the world. Its spiral arms are cauldrons where new stars form out of gas that falls in like a fine rain from intergalactic space. Stars like the Sun will some day die, but in Orion and Taurus freshly minted stars are switching on for the first time. The bright fizz of supernovae is a sideshow; most stars die modestly and leave behind fading embers. Stellar lifetime is a strong function of mass because low mass stars are misers with their hydrogen. The lowest mass stars will eke out a dim existence for over a trillion years.

The end of the Milky Way will come slowly, in a stellar lockdown. Massive stars live short lives and die explosively as supernovae – leaving behind a neutron star or a black hole, neither of which emits any light. Stars like the Sun and those less massive will die as white dwarfs – that is, as slowly cooling, carbon-rich embers. Gradually the cycle of star birth and death will be irrevocably broken. More and more mass will be trapped in compact stellar remnants or cooling white dwarfs. In galaxies across the universe the lights will gradually go out, and after tens of trillions of years the universe will have faded to black. But as bleak as it sounds, the end of starlight doesn't mean life must end.

A star shines by converting a tiny proportion of the energy locked in pure matter into radiation. The ultimate source of starlight is gravitational energy. There are many ways other than fusion to turn gravitational energy into heat or radiation, so even after the stars have all faded enterprising civilisations could live by harnessing the energy of black holes. New artificial stars could be created if nostalgia dictated.

*****

Fifteen years ago, it was discovered that the cosmic expansion is getting faster. The cause is inferred to be dark energy – a manifestation of the pure vacuum of space that has an effect opposite to gravity: it repels rather than attracts. Its existence was indicated by the fact that distant supernovae are fainter than expected in a decelerating universe. Dark energy is an embarrassment: fundamental theories don't predict it, and no one knows how a pure vacuum can have such a bizarre property.

In some theories, dark energy is not the cosmological "constant" of Einstein's original formulation, but varies over time and space. If dark energy grows, it will cause the universe to unravel in about 20 billion years in a crescendo called the "Big Rip". First galaxies, then stars, and finally atoms will be torn asunder by dark energy. Nothing can survive; it's an outcome of crushing finality.

Absent the big rip, cosmic acceleration will steadily remove galaxies from view. After 100 billion years, most galaxies will recede faster than the speed of light, leaving frozen final images on the edge of our horizon as if at the boundary of a black hole. The Milky Way and Andromeda galaxies will merge and our view of the universe will end at the edge of this super-galaxy. On even longer time scales, familiar gravitational structures become unglued. In about 10^15 years, planets detach from their dead stars and drift through interstellar space. In about 10^19 years stars detach from galaxies and float off into intergalactic space. In most theories that unify fundamental particles in terms of a single super-force, the proton is not stable and will decay in something like 10^35 years. This vast time scale is to the age of the universe what the age of the universe is to a millisecond.

The decay of protons heralds a final drawn-out phase of disintegration in the universe, as everything falls apart. After protons decay, there are no stable atoms, presenting a challenge for life. The curtain falls with the slow evaporation of black holes by a process called Hawking radiation. The largest black holes evaporate on the inconceivable time scale of 10^98 years. We imagine the last inhabitants of the universe huddled around the evaporative glow of gamma rays from the last black hole, telling timeless stories about time. It was fun while it lasted.