Writing guitar licks can sometimes become frustrating as there are many concepts to consider. One is the environment which the melodic line will fit, such as style, tempo, and harmonic function.
If for instance you created a lick over a I - IV - ii - V progression it would be limited to only that progression or a progression which has chords substituted from the same functional family. This means that if you then wanted to play the same lick over a different chord progression such as I - V/iii - vi - IV, it wouldn't work as the harmony functions in a different direction, plus it has non-diatonic harmony such as the secondary dominant "V/iii," thus, you can never create universal guitar licks. However, there are a lot of songs that share the same or family relative chord progressions such as "Sweet Home Alabama" and "Sweet Child O' Mine." So in harmonic theory, you will be able to create a lick which will fit into both of these songs; although there will be stylistic contrasts, but if you are working on one particular song, then trying to create some licks would be an appropriate action to take.
Once you have the song you are working on, you must find the chords, then work out how the harmony functions. If you haven't done much on harmony, it doesn't matter - just choose a song with a basic chord progression. Understanding the harmonic function is vital as it will help you distinguish how you will melodically approach each chord in its own functional purpose. It will also show which extensions will harmonically work. For example; if you are in G major and you find that the harmonic movement is leading into non-diatonic territory, you obviously can't use the same extensions as you would in G major as the harmony may be moving towards Db major. Then you must take into consideration the style of the piece; is it rock, country, or jazz etc...
Style is possibly the most complex concept to explain. It isn't black and white. Think of style as looking at a rainbow colour chart and asking yourself "What is green?"; it's the same in musical styles. However, most do share some similarities, but none are the same. The best thing to gain experience for this would be to listen to a wide array of music. Take inspiration from others, but don't directly copy as this isn't progression on your half. Don't follow stylistic "rules" rigidly - like I demonstrated with the colour chart explanation; styles are just general guidelines. Study what other guitarists have done, how they approached melody, phrasing, and rhythm in context to the harmonic functions, however simple they may seem.
It is worth mentioning that there is also your own personal practical limitations, such as technique, theory, ear training etc... One of the most important practical/theoretical concepts to understand before starting any lick is the "CAGED System." The CAGED system is a method of linking up all scales through logical chord pattens - you can find information about this, although it is confusing at first, so I may be making a tutorial to help with that later on. Once you've overcome this "obstacle" the most logical step would be to improve technique; play each one of your "CAGED box shapes" in patterns such as 1-3-2-4-3-5-4-6-5-7-6-8, 1-4-5-2-3-6-7-4-5-8, 1-2-3-2-3-4-3-4-5-4-5-6-5-6-7-6-7-8-7-8-2 etc...
After this, you would have the knowledge of how to create licks, but gaining insight is something that cannot be taught. Music is an art, so creative thought processes are foremost.