Content producers (e.g. ESPN) charge cable companies (e.g. Comcast) for their content. ESPN is the most expensive, and comes in around $6 / user / month. Some of this is passed on to consumers in their subscription fee. But the cable companies are also given the chance to offset the cost, or make additional profit, in the form of a 2 minute per hour commercial allocation. This means the cable company can sell ads itself for two minutes of commercials on ESPN every hour - to the extent that someone is watching.
Communication on the internet is (generally) one-to-one, meaning a message is sent by a sender and routed to a recipient through a web of routers. Cable looks more like a family tree, where the same content is broadcasted in one direction to all the downstream recipients. So if you're the cable company, the simple option would be selling ads against stations, groups of shows, or the like. This would mean that the flow of content that includes the content for the program would include the ad that you sold in your allocated 2 minutes - and everyone would see it. This does, of course, require a substantial sales team and a lot of effort to make sure you're selling against all the shows that could air on every station at any time. Naturally, there's a lot unsold inventory, and a lot of room for improvement. There are a lot of companies that try to help here - Simulmedia being a relative well known startup in the space.
When you buy a DVR, your hard drive space is partitioned. A portion is available to you for your TV shows, an a portion is taken by your cable company to store advertisements. Your cable company knows the shows you watch, your family demographics, your address, etc. So rather than selling against content, it can sell against audience. Your DVR is programmed to call home and download 10-12 ads that the cable company has sold against your demographic. This happens well in advance - generally 3-4 days. The advance time is required because there's limited reliability in the cable context. Satellite TV companies, for example, cannot send any customized data, so it's downloaded through the telephone line that also connects to the box. So when the time comes for the cable company's allocated 2 minutes, it can play the previously-downloaded ads, or it can play the default ads on the stream if there's nothing on the local machine. This process is called dynamic ad insertion and it's a small but growing part of the ecosystem (< 5%). Substantial challenges include the actual physical infrastructure (cable boxes, flexible cable systems). You might ask why, if the cable companies can make more from DVRs through dynamic ad insertion, do they charge more to rent DVRs than more simple cable boxes? The answer, simply, is because they can make even more money that way. You want the DVR, you don't (didn't) know that it was also being used to make the company more ads, and that might not really matter in the value prop of what a DVR brings to you.
There are a couple interesting side notes to this. The first is that there's a battle in congress about making it so any cable box can work on any cable system, instead of the one the company rents to you. It used to be the case that you had to buy your telephone from AT&T, but congress said that that was dumb, and it looks like they may use a similar rationale here. In turn, this would impact the economics of TV and broadband distribution, so it's decidedly less simply than the phone question. Google's broadband plans start from scratch and address many of the legacy questions plaguing existing carriers, meaning they have significantly improved opportunities for programmatic monetization.