Getting cold in the middle of the night, or so hot and sweaty that you can't sleep, doesn't necessarily mean you didn't buy a good sleeping bag. More likely, you just bought the wrong sleeping bag. Since there are literally hundreds of models of sleeping bags available, perhaps even thousands, this happens more often than you would think.
While it is always important to consider how you plan to use equipment, sleeping bags tend to be more of a general-purpose general purchase. However, if you are planning on serious winter camping (in very cold weather) or mountaineering, you may well want to invest in a winter bag that is rated to twenty or thirty degrees below zero, in addition to a lighter bag for three-season use. Alternatively, some bags offer zip-out linings, so you have a double bag for cold weather, and your choice of the lining or outer bag for warmer conditions.
In general, three-season bags are rated to about 20 degrees above zero, which will work for warm conditions and usually be sufficient for brisk spring and fall nights. However, remember that there is no universal standard for bag rating. Also, people sleep at different temperatures, so while a 20-degree bag might keep your companion warm on a cold night, you might freeze in the same bag.
When you buy a bag, a good rule of thumb is to think about the coldest condition you might experience, and then drop down ten or twenty degrees. Keep in mind that it is more difficult to stay warm in an insufficiently insulated bag than it is to vent a bag designed for cooler temperatures. A bag rated to zero is usually a good choice, since it will keep you warm on unexpectedly cold nights, but can be zipped open for venting.
Wet Bags. Wet bags aren't as warm as dry bags. A simple means of keeping your sleeping bag dry is to place it inside a plastic garbage bag. (You can then use the bag for garbage on your hike out). If you have a down bag, always allow the bag to dry before you place it in your stuff sack. If possible, hang any bag outside of your tent in the morning, until you are sure the moisture has wicked away. The caveat here is to avoid excessive sunlight, so either find a shady place or pack up your bag before the day gets hot. Most shell fabrics are treated with a DWR (durable water repellency coating). Check the bag's care manual for when to reapply a waterproof booster.
Eating and Drinking also affect sleeping. Remember to eat and drink before going to bed. The warmer the food, the warmer you'll start out. If you go to bed cold, it is more difficult to warm up your bag. A brisk walk before bed, or a few stretches in the tent, can help to boost your body temperature. If you find you get cold sleeping at home, you'll probably have the same difficulties on the trail, so invest in a warm bag.
Tent or Bivy Sack. A tent or bivy sack will add at least 10 degrees F to the warmth of your bag. You can also invest in an outer bag or bag liner to boost your bag's capabilities for those frigid occasions.
Sleeping Pad. Always use a sleeping pad. It will help prevent conductive heat loss between you and the ground, as well as providing a more comfortable berth.
Extra Clothing. Bring along a light hat, gloves, and a pair of warm socks, even for summer camping. They can add crucial warmth on cold nights. Don't sleep in the clothes you wore hiking during the day, since they may be wet with sweat. Bring along a layer of warm, comfortable clothes to wear inside your bag, but none so bulky that they restrict circulation.
Tent Placement. Consider the placement of your tent or sleeping pad. Low areas may become pockets for cold air. High elevations, especially when exposed to wind, can also present a chilly problem. If possible, select a sleeping spot that is protected from the wind and weather.
Bag Handling. Shake and fluff your bag several times before jumping in. This is especially important for down bags, which may need time to fully loft. Some manufacturers recommend placing your bag (synthetic or down) in a warm dryer before embarking on a camping trip. The tumbling will help to fluff the bag, and will rid it of any residual moisture.
Stuffing your Bag decreases the life of the loft. Don't store your bag in a stuff sack, rather hang it in a cool dry place. When you do stuff your bag for travel, do it gently, and avoid leaving the stuffed bag in a hot car.