First, always handle new strings with care. If you bend or put a kink in a new string when you're taking it out of the pack, you can create a weak spot in the string that will snap in the middle of your first solo every time.
Breaking strings on electric guitars is usually caused by two things: technique and worn bridge saddles. When you pick the strings, they move slightly across the saddle before sounding. The length of travel depends on where and how you pick the string. A tiny burr or groove in the bridge saddles can catch the string and cause it to break. If you're breaking a lot of strings at the bridge, it's probably caused by a funky saddle. Replacement saddles are relatively inexpensive and easy to install at home. Make sure replacement saddles are stainless steel or some other hard metal designed to reduce string breakage. If replacement saddles don't solve the problem, you'll either have to get used to busted strings or change your playing style.
On acoustic guitars, there can be sharp edges near the end of the pin that catch the strings. Sometimes an emory board or fine sandpaper can be carefully used to fix this problem.