Solos in a song are played over the top of chords played by the rhythm instruments; one of the most important technical aspects of a good solo is making sure it goes with the notes of the chords. To master this, practice solos that are largely based on the notes of the chords going on behind you. This type of bare-bones solos will be boring, but they won't have any bad notes. Once you master this, incorporate more interesting notes and rhythm between those notes, but continue to rely heavily on the chordal notes in your melodies.
Learn classic guitar solos by ear to help you master your own soloing. The idea isn't to copy these solos, but to learn what makes them outstanding. Memorizing every note and inflection will influence your playing and help you absorb elements of the music subconsciously. Some classics to start with include Jimmy Page's solo in "Stairway to Heaven," Gary Rossington's licks in "Free Bird," Jimi Hendrix's stuff from "All Along The Watchtower," and Don Felder's guitar playing in "Hotel California."
Practice free improvisation (not as part of a song and without any backup band or other accompaniment) regularly to improve your ability to play a more structured solo in an ensemble. Strive to improvise without any self-censoring or judgment about what you're playing; just see what comes out and experiment as wildly as possible. This will help your creativity.
Scales are an essential part of any improvising soloist's vocabulary. Learn to play your scales by memory and with speed and facility. This encourages your ability to make the sound in your head come out of your guitar, since your fingers will "know" the movement through the key you're playing in.
Use the main melody, or the "head," of the song as the basis for at least part of your solo. This means playing something that sounds like the melody, but not quite. Common ways to do this may include playing something that sounds like the melody, but then transforms into something with the same rhythm as the melody, but with different notes or playing the same notes with a different rhythm.