It's a less obvious method of doing long division.
Effectively you work out how many times the highest power of the divisor (the bit on the bottom) goes into the highest power of the dividend (the bit on the top).
In this case it's "x" times.
Then you multiply the bottom by that value, and subtract it from the top.
Thus you have "x + ..." where what you've got left after you've subtracted that "x" is a polynomial of a reduced index (because you've extracted as many "x^2 - x - 2" factors out as you can.
Then you do the same thing again, each time you're reducing the order of the polynomial at the top till it has a smaller order than the one at the bottom.
Notice how "x^3" becomes "x(x^2 - x - 2) + x^2 + 2" in the first line, multiply it out and you see it comes to x^3.
Oh yeah, I notice the "polynomial long division" at the bottom as well, that's doing in symbols what I did with words above.