What would be the first thing which will render the Earth uninhabitable?
I'll try to put time scales on each of these events.


The Sun as a red giant - 5 to 6 billion years$^1$

The Sun is currently on the main sequence, which means that it's a "full grown" star - think of it as being middle-aged. It's been on the main sequence for around 4 to 5 billion years, and in 5 to 6 billion years, it will leave the main sequence.

Most yellow (i.e. spectral class G-ish) stars spend about 10 billion years on the main sequence. Then, they swell up as red giants. The Sun is just over halfway through the main sequence, so it has about 5 to 6 billion years left. After 5.4 billion years, it will grow. Here's an image showing how its radius (and other factors) will increase:



The Sun won't swallow the inner planets up right away - see how it takes a while for it to grow - but it will happen eventually. It will take another billion years to reach a radius 200 times that of its current size. It will enter the AGB phase, where it will finally grow enough to swallow the inner planets. It's uncertain as to whether or not Earth will be destroyed, but it will be heated up so much that liquid water will disappear and the planet will be uninhabitable.

NASA has an article on a Sun-like star swallowing up one of its inner planets, just as the Sun may do. The original paper can be found here.
Supernova/nova - Never

There are two types of supernovae: Type II and Type I. Type II supernovae are characterized by hydrogen; Type I supernovae have no hydrogen. You've probably heard of Type II supernovae when you learned about supernovae, and they're the more well-known. They're the result of the collapse of the core of a massive star. However, the progenitor, has t be much larger than the Sun - usually at least 8 solar masses.

Type I supernovae have no hydrogen. There are two main classes: Type Ia and Type Ib/c. The first category are the result of white dwarfs accreting mass from a companion star. The Sun has no binary companion, so even though it will become a white dwarf, it will never undergo this kind of supernova. Type Ib/c also result from core collapse, just like Type II. However, their progenitors are unstable, violent stars, typically Wolf-Rayet stars. Our Sun will fortunately never become one of those!

A nova is small compared to a supernova. It, like Type Ia supernovae, may be the result of a white dwarf accreting matter from a companion. Once again, though, the Sun has no companion star to accrete matter from.
Magnetosphere loss - Never

The Earth's magnetic field is very important, because it shields us from the solar wind. Without it, life on Earth probably couldn't exist! Its disappearance would not be good for us.

Fortunately, it doesn't appear that that will ever happen.

The Earth's magnetic field is believed to be caused by the movement of fluids in Earth's core. Wikipedia lists three conditions necessary for the existence of the magnetic field:


  
  An electrically conductive fluid medium
  Kinetic energy provided by planetary rotation
  An internal energy source to drive convective motions within the fluid.
  


The fluid in the Earth's core will never go away, and so long as the planet keeps rotating, that kinetic energy will always be there. So will the Coriolis effect, which also helps circulate the fluid and control the magnetic field. The source of the energy that keeps the material hot is due in part to tidal forces between the Earth and other bodies, such as the Moon and the Sun.
Collision - Not for a long time

The Earth has had one major collision throughout its life, and that spawned the moon. This idea is known as the giant impact hypothesis. When the solar system - and Earth - was young, the Sun had a disk of small pre-planetary bodies, known as protoplanets. There were more protoplanets than there are planets today, because many collided to form the planets.

The hypothesized protoplanet, named Theia, is generally described as Mars-like. It didn't hit the Earth head-on, but rather hit it an a 45 degree angle. The result was that Theia was destroyed and some of Earth was ejected. Much of this material came back to Earth, but some stayed and formed the Moon.

There aren't any more protoplanets, but there are still some large asteroids. The biggest is Ceres, which is tiny compared to Theia. Here's a picture of Earth, the Moon and Ceres (bottom left):



We're not likely to see another giant impact again. The other planets all have stable orbits, and the Moon's actually receding from Earth due to tidal forces. Sure, asteroid impacts are a possibility, but the possibility of a doomsday asteroid is tiny. Even a body like the one that killed the dinosaurs wouldn't extinguish all life. The Jet Propulsion Laboratory has information on the likelihood of such a catastrophic event (not a high one!).
Gamma-ray burst - Probably never

I wrote an answer on Astronomy about this a while ago. If I may, I'll quote from that answer:


  Okay, back to the topic at hand. Wikipedia, of course, has a short tidbit on the frequency of gamma-ray bursts effecting Earth:
  
  
    Estimating the exact rate at which GRBs occur is difficult, but for a galaxy of approximately the same size as the Milky Way, the expected rate (for long-duration GRBs) is about one burst every 100,000 to 1,000,000 years. Only a small percentage of these would be beamed towards Earth. Estimates of rate of occurrence of short-duration GRBs are even more uncertain because of the unknown degree of collimation, but are probably comparable.
  
  
  'A small percentage' isn't too specific. The BBC article that is cited in this passage says the following:
  
  
    Observations of deep space suggest that gamma ray-bursts are rare. They are thought to happen at the most every 10,000 years per galaxy, and at the least every million years per galaxy.
  
  
  However, the 'small percentage' is never elaborated on.





$^1$An estimate

Earth will become uninhabitable long before the Sun swallows it up - if it is swallowed up - because its surface temperature will be so high life cannot exist. I wanted to see if I could figure out when that would be.

You can approximate the temperature of a planet by using the formula for effective temperature:
$$T=\left(\frac{L(1-a)}{16 \pi \sigma D^2} \right)^{\frac{1}{4}}$$
where $L$ is the star's luminosity, $a$ is the planet's albedo $\sigma$ is the Stefan-Boltzmann constant, and $D$ is the distance from the planet to the star.

The current mean temperature of Earth is about 15 degrees Celsius (288 K). However, this is the average surface temperature. Fortunately, the equation given for surface temperature is also of the form
$$T=C L^{\frac{1}{4}}$$
where $C$ is a constant. This means that
$$T^4 \propto L$$
The boiling point of water is 373 K, which is 1.295 times the current surface temperature. If we plug this in,
$$L= \propto (1.295)^{\frac{1}{4}} \approx. 2.81$$
So the luminosity of the Sun would have to be about 2.81 times the current luminosity of the Sun. This value isn't on the graph I gave at the top, though it appears to be about 7 billion years from now. Wikipedia gives this point as only 1 billion years from now, because of a greenhouse effect. So we've only got 1 billion years to live!