To flesh out Nick's answer a bit: the core concept behind the DDA algorithm (which works just as well in three dimensions) is that for each axis of your grid you keep track of the next 'crossing point' for that axis in terms of your line parameter; each step of the algorithm consists of finding which axis has the next crossing point (which is a simple compare in two dimensions), taking the appropriate step, and updating the next-crossing values for each axis.

The line here can be written as '(x,y) = (x0,y0)+t*(m,n)', where m=x1-x0 and n=y1-y0. If the dimensions of a grid cell are gx and gy, then dx — the distance (in terms of the t parameter) that it takes to cross one grid cell — can be found with a bit of quick algebra: from the pair of equations x = x0+m*t, (x+gx) = x0+m*(t+dx) we get gx = m*dx, or in other words dx=gx/m. Likewise, dy=gy/n. The algorithm keeps track of next_x (the distance to the next red point along the line) and next_y (the distance to the next blue point along the line) and updates them every time it hits another crossing, so the central loop looks something like this:
while ( cur_t < t_max) {
if ( next_x < next_y ) {
cell_x++;
cur_t += next_x;
next_y -= next_x;
next_x = dx;
} else {
cell_y++;
cur_t += next_y;
next_x -= next_y;
next_y = dy;
}
// Process the cell (cell_x, cell_y)
}
Note that this code is missing a lot of the details - it doesn't tell you how to initialize next_x and next_y, for instance. There are ways of eliminating most of the divides, making it easier to handle special cases like vertical and horizontal lines. Whether you increment or decrement cell_x and cell_y depends on which quadrant your line is in - note that for my example line, you'd actually be decreasing cell_x each tick, since m (x1-x0) is negative. You also have to decide how you're going to handle cases where your line goes precisely through the corner between cells, rather than transitioning on an edge; there are lots of small details that can go wrong, and it needs a lot of testing. Still, hopefully this will give you a picture of what the core idea of the algorithm is.