Forget Armageddon and Deep Impact. Movie physics is not real. It's a movie, after all.
Should some large dinosaur killer class asteroid or comet be on a trajectory that eventually impacts the Earth, our only hope is to detect that object decades in advance. A large number of options exist given adequate advance warning.
No options exist were we to detect that large body just a few weeks before impact. Blowing it up may well result in even more damage than had we just let it be. It's still going to hit us, but now we'll have lots of pieces covered with radioactive debris heading toward us. Do it just wrong and we'll lose our atmosphere.
Edit
We would need a huge amount of lead time to divert an extinction level asteroid. A hundred years might do it. Those once in a hundred million year or so events are not the key worry. We should be able to see those a hundred years in advance. It's the Tunguska-style events (50 meter diameter asteroid) that occur every few hundred year or so years (some say more, others less) and the Ch'ing-yang-style events (100 meter diameter asteroid) that occur every few thousand years (once again, some say more, others less) that scare people.
To put things in perspective, we never saw the Chelyabinsk meteor (< 20 meter diameter asteroid) of February, 2013 before it hit Chelyabinsk. Those things are very numerous, but not so damaging. Lots of people were injured, lots of buildings were damaged, lots of money was lost, but no one died. However, damage is roughly proportional to the cube of the linear size. A fifty meter asteroid would cause over twenty times the damage of that Chelyabinsk meteor; a one hundred meter asteroid, almost 200 times the damage. A Tunguska-style event over the center of a major metropolitan area would kill hundreds of thousands of people. The death toll would be in the tens of millions with a Ch'ing-yang-style event.
The problem with these relatively high frequency events is that even with advanced detection capabilities, we'd be lucky to have a decade of advanced warning. That rules out things such as the gravity tractor in WetSavannaAnimal aka Rod Vance's answer. That technology requires multiple decades using existing technology, and with but a decade's advanced warning, we wouldn't have time to do research.
We would need a few years to nail down the orbit so we could intercept it, a few years to prepare for the mission, a year or so to wait for the launch window to open, and a few more years for the vehicle to get there. Some of those things can be done in parallel, but even with that, a decade doesn't give much time.
To make matters worse, we wouldn't have a decade. We would have at most eight years. Even with nuclear weapons, the weapon would have to explode about two years prior to impact to change that impact into a miss. The delta V needed to change an impact into a miss is a highly non-linear function of time before impact. If the action occurs but a few weeks prior to impact, the energy needed to turn that impact into a miss would exceed the yearly energy consumption by all of humanity. The curve is nearly exponential with short time before impact. The knee in the curve is around 400 or 500 days. That means we would need to take final action a couple of years or so prior to impact.
The most reliable way to use a nuclear weapon to divert an asteroid is to make it explode at a short standoff distance from the asteroid. This wastes over half of the energy from the explosion, but it is something we know how to do. It is "Technology Readiness Level 9", or TRL 9. The explosion bathes that side of the asteroid with gamma rays, X-rays, and high energy nuclides. That near instantaneous pulse of energy vaporizes a small layer of one side of the asteroid. It's this secondary explosion rather than the primary explosion of the bomb that changes the asteroid's trajectory.
A nice side effect of performing this standoff explosion a couple of years prior to impact is that even if the asteroid is a rubble pile and is blown apart, it will reform as a rubble pile within a year or so. The primary explosion doesn't do much to the asteroid; that explosion occurs in the vacuum of space. The secondary explosion ejects that thin layer of vaporized asteroid, but the kick given to the rest is less than escape velocity.
Alternatives to a nuclear standoff explosion
There are a couple of other approaches involving nuclear devices, an explosion at the surface and an explosion below the surface. The surface level explosion would impart more energy to the asteroid, a subsurface explosion, even more. There are multiple challenges with both approaches that reduce the readiness level well below TRL 9.
Another approach is kinetic impactors. We've done that before; for example, we intentionally dropped an expired satellite into a crater near the Moon's South Pole to determine if those polar lunar craters contained ice. They do. The problem with kinetic impactors is that they don't have near the impact of a nuclear bomb, and rubble piles and rotation present even more challenges. We would need to use a number of impactors working in concert with one another, once again reducing the readiness to well below TRL 9.
The gravity tractors mentioned by WetSavanna fall into the broad class of slow push / slow pull approaches, with the gravity tractor being at the top with regard to readiness. There have been a number of papers published on gravity tractors, with a wide range of time spans needed to generate the necessary delta V. The papers that pay close attention to technology, to control theory, to the very lumpy gravity fields of small mass (< 1 km diameter) asteroids, and to the quirky rotational behavior of these asteroids all say that the rockets must fire for many years to generate the requisite delta V. The papers that say that only a few weeks of thrusting is needed are written by people who ignore technology issues, who don't know control theory, who don't know the nasty consequences of those lumpy gravity fields, and who don't know the nasty consequences of the polhode rolling without slipping on the herpolhode lying in the invariable plane.
There are a number of other approaches as well. Most of them have some critical piece that is somewhere between TRL 1 to TRL 3. Technologies oftentimes take decades make it out of that low TRL quagmire. That's not good when millions of lives might be lost within a decade.
References
The best reference to date is the 2006-2007 study performed by NASA. It's bit outdated, but it does cover the main concepts quite nicely. NASA's 2007 Report to Congress is a bit brief and shorts on the science and engineering a bit. The 2006 Near-Earth Object Survey and Deflection Study preliminary study report contains a lot of substance. Section 6 (pages 72-119) cover deflection alternatives.
Another widely used reference that summarizes available options is Brent Barbee's Master's thesis: Barbee, B (2005), Mission Planning for the Mitigation of Hazardous Near Earth Objects. As is the case with the NASA report, nuclear standoff is the clear winner, both in terms of delivered delta V and not requiring any technological breakthroughs.
For a set of more recent references, look to the Asteroid Deflection Research Center at Iowa State University. A summary page at nasa.gov describes their most recent research. Details can be found at the Asteroid Deflection Research Center web site.