We spend about 1/3 of our day sleeping. Our sleeping posture is just as important, if not more important, than our daily posture, whether sitting at a desk or standing. Basic understanding of posture is that we should "sit up straight" or "stand up straight." At least this is what I was always told by elders when I was growing up.
Obviously, there is more that goes into it, but that is the basic idea. The same goes with sleeping. Think about standing up straight with feet at shoulder width, slight bend in the knees, facing forward, ears in line with the shoulders, arms by your side with palms facing forward (although most of us will have our palms facing our side or even behind us -- this is a topic for another day). You are basically standing in "anatomic position."
We want to try to stay as close to this as possible when sleeping, which could potentially be very tough since it is not easy to change one's sleeping habits. The basic idea is to keep your body as neutral as possible. If laying on your back, have your head supported just enough so that is not over-extended with your chin jutting out or hyper-flexed with your chin pushing down towards your chest (a cervical pillow can help -- but its easy to buy the wrong kind if you don't know what you are looking for, but that's another topic for another day). Try to keep your arms by your side, not flexed into your chest with your wrists flexed (always try to keep the wrists straight) or behind your head. Try using a small pillow under the knees. Just like standing, we want to keep our knees slightly flexed while sleeping.
If you are a side sleeper or tend to roll over to one side or both sides throughout the night, the same concepts hold true. You may need an extra pillow to support the head while on your side to keep your head supported in neutral position, not leaned too far to the side your are laying on or being jammed into the opposite shoulder that is up in the air. Try to avoid pulling your arms into your chest with your wrists flexed (especially if you work on a computer all day, try to keep your wrists straight since they are constantly being flexed all day). You will have to bend your elbows or flex your shoulder in this position so that you don't fall onto your stomach. The most important part if you have low back or hip pain is to try to keep both legs shoulder width apart by using a pillow between them, and try not to flex your top leg if your other one is straight. We are better off slightly flexing both hips and knees then having one straight and one flexed since this puts uneven stress on the pelvis and lumbar spine. Keep the legs as straight as possible, as too much flexion will put constant pulling stress on the low back and pelvis and jam your hips (and hip flexors). If you sit at a desk an/or work at a computer, then you are already in that position most of the day. We want to try to avoid that position while we sleep, as the hips and back don't get the rest they need, increasing our chances of having chronic low back and hip problems.
Never sleep on your stomach! (will go into more detail at a later date)