Many lead solos you hear on recordings are not actually improvised, but have been worked out in advance.
In such examples, the lead solo is said to be composed, rather than improvised (in "live" group performances, however, there is likely to be more improvising involved as lead solos are often extended).
One of the main ways to improvise is to create variations based on established melodic and rhythmic structures. Some ways to achieve this are listed here.
The best way to develop your improvising ability is to play with other musicians as much as possible. If you are jamming with another guitarist, you can take turns at playing lead and rhythm. When you are playing rhythm, listen to the lead player and try to provide a solid base for them to improvise over. When your turn comes to play lead, see if you can use some of the ideas of the other player and alter them to create your own licks. Playing along with recordings is another valuable way of developing your improvisation skills. When you improvise a solo, try to play something that makes sense in relation to what has gone before it and where you are going next. Playing a solo should be like telling a story rather than a series of unrelated licks. Listen to players you admire and try to work out what makes them sound the way they do. Most of the great lead players have learned a lot by copying licks and solos from recordings and then developed their own style from what they have learned. Copying solos from recordings is dealt with in appendix four.