Why/How is the B-52 still in service?
Three reasons:


The B-52 stayed useful by taking on "easier" roles as it aged.


It actually started life as a high-performance penetrator, relying on speed and altitude to stay safe. Think Early Cold War, shortly after WWII.
When ground-launched missiles improved, the B-52 was forced into flying low-altitude penetration underneath radars (a role for which it was NOT designed and rapidly wore out the airframe) accompanied by a host of other aircraft to protect it and help it penetrate defenses. Despite their escorts, we lost low-dozens of B-52s in Vietnam.
Later in the Cold War, the B-52 was completely outmatched by air defenses (fighters, SAMs, etc), and had little hope of penetrating defenses (at either low or high altitude) to drop nuclear bombs. Instead, it was relegated to flying in friendly/neutral airspace, lobbing long-range nuclear cruise missiles into the Soviet heartland (a tactic they feared more, as it turned out, than manned low-level penetrators). It was basically a missile bus.
In the Gulf War, the B-52 only flew after air defenses had already been defeated by other aircraft (F-16s, F-15s, F-111s, etc). It was basically a bomb truck.
During ALLIED FORCE (1999), "B-1Bs and other conventional (non-stealthy) aircraft could not survive in the face of double digit SAMs, even when employing advanced decoys and other countermeasures."
The B-1 program, btw, went through a similar progession, starting out as a high-performance penetrator,  then forced into even lower-altitude penetration, then also relegated to bomb/missile trucking, pushed back by AAA, MANPADS, SAMs, look-down shoot-down radar, improved Soviet interceptors, and Soviet AWACS. In fact, the B-1 and B-52 fly largely* the same missions today. 


*There are some important differences. The B-1 is no longer nuclear capable (START compliance), leaving that mission to the 20 B-2s and ~40+ ALCM-armed B-52s. The B-1 can also reposition more quickly, lending it to CAS role: it dropped 37% of the then-total ordnance on ISIS (2x that of the A-10)(see source) in OIR before they returning stateside for upgrades and were relieved by B-52s. (And, yes, the B-1 does CAS.)
Overall, the B-1s fly ~330-450 hours/yr, the B-2 ~230-290 hours/yr, and the B-52s ~200-220 hours/yr (2008-2012). The B-1 and B-52 cost roughly the same per hour (the B-1 has higher fixed costs but lower operational CPFH). 

In 40 years, the B-52 went from being the US' primary strategic tool against another superpower to being outclassed by small nations. 
Bomb/missile trucking is an "easy" job, but far from ideal. You can't bomb your targets until the air defenses have already been defeated---after the "first week" of a war, the most critical phase, when you need them the most. In other words, the B-52 (and B-1) are relatively easy to shoot down, so they're not nearly as useful as they used to be. You can't fly them unless many of the toughest enemies are already dead. So you can't use them as often as you want.
@MSalters phrased it quite nicely: the reason the B-52 (and B-1) hasn't changed is because other aircraft are doing the hard work for them (tearing down enemy defenses), and such aircraft have advanced immensely. But that amount of support is quite resource intensive, so if you wanted to fly B-52s in the "first week" when enemy defenses are still effective, you'll need a lot of help: (Image source: Beyond the "Bomber": The New Long-Range Sensor-Shooter Aircraft and United States National Security - Lieutenant General David A. Deptula, USAF (Ret.), 2015.) The same was true with strike packages during Desert Storm, eg only a third of the aircraft dropped bombs while the rest provided support (jamming, anti-radiation, top cover, etc).

We didn't build enough B-1s and B-2s to replace it, so we kept some B-52s to fill in the gaps (we don't have any other suitable heavy bombers in inventory, this is the only choice).


The 100 B-1Bs were just a stopgap until the B-2 showed up, of which we meant to build 132 (cut to 21 airframes). Had the Cold War continued, the B-2 would have comprised the bulk of today's bomber force.

We were busy buying new fighters and cargo planes.


The official, stated goal is to recapitalize the entire bomber fleet (less the B-2) because it's increasingly obsolete. That's worth repeating: the B-1 and B-52 are near obsolete. They're not nearly capable enough. The next bomber isn't another B-1, B-52, or modded 747 because we don't want to maintain the same capabilities. We want more capability--much more--than what we currently have. We've only waited this long because replacing the airlift and fighter fleets took priority.
The LRS-B (new stealth bomber; formerly NGLRS) will replace the B-52 starting around next decade. The contract is expected to be awarded very soon. The Pentagon briefed a select group earlier this month. Looks very promising---well managed, on budget, very capable, and surprisingly mature. 


UPDATE: The B-21 (ex-LRS-B, ex-NGLRS) was indeed awarded 27 Oct 2015, a month and a half after this post was originally written. Very little is known, but it is indeed a stealthy bomber (the official render closely resembles the outer mold line of early B-2 iterations before the redesign). And it is indeed on time and under budget by ~9% (APUC = \$511M vs. the \$550M requirement, FY10\$).




In short, we still fly the B-52 today because we don't have a choice, not because we want to.



Parting thoughts: 

I want to emphasize that the B-52 did not remain effective in its intended role (penetrating the USSR on Day 1), upgrades notwithstanding. 

Yes, the B-52 survived multiple replacements (eg XB-70, B-1A), but not because it was more capable (it wasn't; the B-70/1A went higher/faster/lower/with more payload), but because those potential replacements couldn't outpace Soviet defenses either and so were cancelled in favor of ballistic missiles and cruise missiles... except the B-2, which could outpace Soviet defenses but was curtailed when those Soviet defenses disappeared. The B-52 survived basically because of luck: against the XB-70 and B-1A, it was the incumbent, already bought and paid for; against the B-2, history smiled.

Also, the B-52 did not "survive" the B-1B. The B-1B was never meant to replace all B-52s in the first place. The B-1B was meant to fill an urgent requirement as a stopgap from ~1985-1995 until the B-2 could enter service. Unlike previous B-52 would-be replacements, B-1B production was not cut short: 100 were ordered, and all 100 were delivered, essentially on time and on budget (though that was actually partly the problem).

Thankfully the B-52's range and payload kept it productive during its long service.



Some background.


The USAF operates ~160 bombers:


76 B-52s
63 B-1s
20 B-2s