Generally, I just improvise until I come up with something I like. Then I'll record it. The problem comes when I need to move to the next section in the song. My mind stays locked into a particular pattern and I have difficulty making the next part not sound too similar to the first. When I make a point to make it different, I end up telling myself that it doesn't fit. Sometimes I need to take a break and come back to it later when my mind gets a chance to "refresh".
My best riffs/songs have always come from my mind rather than my hands, though. To explain...sometimes I'll just come up with a riff in my head at random during the day. Then, I need to sit down and "learn" the part on the guitar. These always end up being better than riffs I create while simply improvising with the guitar in my hands.