How much time of playing and learning guitar do you need, to be able to listen to a small riff and play something similar to it. I find it very hard, how can I train this?
I've been only playing for 6 months.
I like mostly to create my own bluesystuff but sometimes I would like to listen to a song and play it, without watching any tablatures, just for the challenge.

Will this come with time? Do I have to follow a special method?
i wasn't able to listen to a song on the radio and play it for years and years. what helped me alot was learning lots of songs and really listening to how notes interact and hearing different intervals. i think once you can start recognizing intervals and certain notes then you can really do this with some proficiency, but don't expect it any time soon. certainly the best thing you can do to get good at this is to practice doing it. if you want to start now i recommend doing this.

1) pick a song you don't know how to play, something you think you could play pretty quickly though if you read the tab (something simple ala nirvana etc)
2) listen to the song a couple times
3) if you don't know intervals by listening (which at 6 months you probably don't) then guess and say "i think it's these chords"
4) play those chords to see if it sounds like the song (you might think it does when it really doesn't) the purpose of this is to get your brain accustomed to hearing things accurately and interpreting them into the correct notes
5) now play what you thought were those chords along with the recording, most likely you'll realize most or all of the chords you thought it was are wrong. if you're really lucky you'll have picked the right chords but the wrong key (i still do this all the time, i'm usually a half step sharp or flat)
6) NOW play along with the song figuring it out chord by chord by doing this, lets say you don't know what the first chord is for the verse, find the chord closest to it and move it around until you find out what it is, this might take a while and you may end up cycling through all 12 notes before you find it. go through and do this chord by chord until you have the whole song figured out. i recommend nirvana for starters cuz its mostly pretty easy 4 or 5 chord songs so it won't take forever to ear out a song and you can feel some accomplishment without an incredibly amount of frustration.

this is what i did to "tune" my ears so to speak and i'm of the avid belief that if i can do it, anyone can.
Yeah man I've been playing for 5 years and go to college for guitar and just in the last year have I been able to really start doing that.

Doing transcription helps, it trains your ears, and while it's balls hard at first (and seemingly takes forever), you start to recognize patterns and anticipate things and get faster at it, which in turn makes you faster at just hearing something on a CD or the radio and figuring it out.

I think singing everything you hear helps too. I'll hear a song on the radio while I'm driving around at work and just sing it to myself all day, lots of times trying to figure it out with solfege so i know what scale degrees everything is, so i can play it in any key anywhere, but often times i just sing it during the day them come home and quickly figure it out on guitar.
it helps if you can identify the scale, then you will know that that almost all of the notes are going to be from that scale
Ain't nothing quite like a 24 hour transcription session to sharpen up the ears.

I just spend all my spare time transcribing songs. Whenever I find something that grabs me, it gets transcribed. I don't often do whole songs, but guitar solos, riffs, chord progressions, and vocal melodies are all fair game.

Spent a week transcribing all the vocal melodies off of Carole King's 'Tapestry' album. Improved my ear dramatically, as well as my soloing. I find transcribing melodies helps me to think of the guitar more as a vocal instrument rather than in terms of speed. Might not be your thing, but the beauty of learning by ear is it's all up to you. Find bits of music you love, transcribe them, and blend them into your playing. That's how all of the greats from the 60s and 70s learnt to play. Just keep doing it until you can rip out whatever your hearing in your head in a single go.
Try all the methods above. They work over time. You just get familiar with the way the guitar sounds.

Another, and more scholastically accepted method is Aural Perception. This is when your able to listen to scale tones and automatically know the interval/placing. Such as a I III V III I interval or a I IV I interval. I learned Aural Perception on a piano. The teacher would play a few notes in a scale and we'd identify them as Tonic/Super Tonic/Mediant/Sub Dom/Dom/Sub Mediant/Leading and the roman numeral. We did this for major/minor scales + modes and stuff so it really helped. Try looking into it.
Portugal. The Man »–
The first is just getting the rhythem down, just pick a random note, say open E, (or anything that fits with the song), and just bang on it and try to match the rhythem, that eliminates a big problem of ear playing. Once you get the rhythem, even if the notes arent perfect, you atleast get the feeling you're getting somewhere, and as we all know, positivity in guitar playing makes a difference, and eventually you'll just find the right notes with time