It really depends on the situation. You're balancing chainline, frame clearance, spoke angles, and in some cases q-factor (the distance from the pedal mounting face to the frame centerline). How you have to weigh each thing can depend on the bike and what you're doing.
It may be helpful to look at a situation where you have tons of freedom, almost too much. Let's look at the example of putting together a singlespeed road bike starting with an unbuilt standard-axle cup and cone freewheel hub, which you'll be re-configuring to run a BMX freewheel, and some square taper double cranks with all the choices you could want in spindle length.
Using a vernier caliper or rule, and measuring the best you can looking down at the hub shell with one eye closed, you make a simple diagram of the immutable points of the hub with the freewheel screwed on. Left to right, these points are the face of the left cone, the center of the left flange, the center of the right flange, the centerline of the freewheel teeth, and the face of the right cone. Mark the distances from one point to the next. (I've had bad experiences putting freewheel spacers on singlespeeds so I'm discounting their existence here.) Also measure the thickness of the hub locknuts you'll be using.
You then plug in a bottom bracket of any spindle length, put the right crank on with its chainring installed on either side of the spider, and measure what front chainline that spindle length is giving you. Then you measure the spider tab thickness, the chainring thickness, and the amount of frame clearance you have between the ring and the chainstay. Finally measure the frame spacing, aka over-locknut dimension.
In theory and usually in practice, that's all the data you need to figure out the rest.
Well put-together singlespeeds must have essentially perfect chainline, so that's your first priority. When I put together a singlespeed in this manner, usually I decide I want the wheel to be dishless (rim centered between the two flanges, so no spoke tension disparity and maximum strength due to maximum total tension). I let the q-factor fall where it will, as long as it doesn't cause a clearance problem. So the first order of business is to see what things look like to have it set up for perfect chainline and dishless at the same time. To do that, look at your diagram. If the wheel were dishless, the distance from the center point between your two flanges to the center of the cog would be your chainline. You then look at your crank/chainring/clearance data and see if you can achieve that chainline with an existent bottom bracket. Remember you measured the spider and ring thickness so you can see what each position on the crank would give you. Also remember you can use chainring spacers to fudge it up front.
If up front is good, all you need to do is set up the hub. Go back to your diagram and look at the flange-to-flange spacing. Take half of that number and add the left flange to left cone face distance. Let's call that L. Then take half of the over-locknut dimension and subtract L. From that subtract your left locknut thickness. The remaining number you have left is how much spacer you need to have on the left side. Do the same for the right, and that's your ideal hub configuration with the priorities of perfect chainline and dishlessness.
From this example you can probably see how things would change if, for example, you wanted to prioritize a certain q-factor over having the wheel be dishless, or if you only had one BB spindle length to work with and you needed to make the chainline right at the expense of the wheel being dishless, or if you were working with a built wheel and weren't able to re-dish it based on a new hub spacing or if its spoke lengths were limiting how much you could viably re-dish it. The process is the same for multi-speed freewheels and cassettes, only you get much less freedom, and in the case of cassette hubs you sacrifice wheel interchangeability once you start tweaking the hub spacing (relevant for riders that want or need to swap wheels with minimal adjustment.)
When it comes to configuring hubs, in most cases chainline is boss; it really can't be sacrificed without performance suffering so its always the first priority. The exceptions are extremely long-chainstayed bikes (like recumbents for example), where the long chain run makes it matter less, or if you have a triple and decide you're willing to make the small-small combos unusable in exchange for narrower q-factor. But there's not a lot of overlap between those situations and ones where you'll be messing with hub spacing configuration.
If one is just overhauling a rear hub and loses track of which spacers go where, it's true at that point that as long as the wheel dish was correct to start with, the spacer arrangement that makes it correct again will generally be the right one. If the wheel dish wasn't necessarily correct to start with, one will have to do some figuring.