see man sed:
http://linux.die.net/man/1/sed
H will append pattern space to hold space.
/^REM/h will copy lines (beginning with REM) to hold space
$ is an address, the last line
{ } is a block executed at $
g copy hold space to pattern space
p prints the current pattern space
So /^REM/h cleans up the hold space and copies the line into it (only when line begins with REM). Everything before the REM line will be lost.
H will append the actual line to the hold space
and ${g;p;} will at the end of the file copy the appended lines to the pattern space and print it