Driving in sand can be a lot of fun. If you ever get the chance to go dune running in the Sahara or the Empty Quarter, take it. It can also be pretty easy. Hard-packed sand is a fairly good surface for driving and you shouldn't have any dramas as long as you remember not to brake too sharply. Loose sand is a different matter though. Even a great driver can easily get stuck. Dunes or loose patches will trap you unless you keep some momentum, and if you lose it you're probably going to come to a halt pretty quickly. Getting going again can be a challenge.
If you find yourself slowing down on loose sand keep clear of the brakes; let yourself coast to a stop. Braking will push up sand ridges in front of the wheels and also tends to make them dig in, which is going to make it harder to start again. Once you've stopped check the routes out that you plan to take. That's especially important on dunes – it's also a good general rule; never cross the crest of a dune until you've checked it out on foot, because there can be a steep drop on the other side.
If you've stopped without bogging in, try to pull away again with minimum use of the accelerator. Pulling away sharply will probably dig you in and you'll be worse off than before, but easing the vehicle forward usually works If it doesn't, or you've sunk in, some more work will be needed.
If your wheels are sunk into loose sand the chances of you being able to drive out are basically nil. You need to get something solid under the wheels and clear a path for them to get out. Digging might help, unless the sand is loose enough to flow back into the hole. If you have an exhaust jack you can use it to lift that wheel out of the hole it's in then shovel sand underneath it. You have a much better chance of getting moving if the wheels aren't trying to climb out of a hole. You can lower your tyre pressures at this point, although unless you have a compressor that should be a last resort.
If your vehicle's belly is grounded on a mound of sand you'll need to dig it away. If you don't its weight and friction will hold you there like you're glued. A long shovel is best for this. Be very careful when digging under a vehicle on sand. Sand can suddenly shift, potentially letting the vehicle collapse on you, so don't get any part of your body under it unless it's securely jacked.
Whether you've dug ramps for your wheels or lifted them and filled in, it's going to be much easier if you have sand channels. We find Treds or Tough Tracks work very well in most situations, but if you drive on sand a lot you might want to try specialised ones like Sand Trax or Sand Escape Buddies. If you can get these under your wheels they'll give you the traction you need to get moving again. Immediately drive out of the hazard area then go back on foot to retrieve the channels.
If you're still stuck and need pulled out you need to make sure the tow vehicle has a safe approach area, or it risks getting stuck too. A winch is ideal, because it can pull you out from the nearest patch of hard ground. Try to pull from the front or rear; any sideways pull will be a lot harder and can upset wheel tracking or even damage tyres.
Recovering a 4wd that's trapped in sand is hard work, but it's almost always possible once you've done enough digging. Of course it's best to avoid it in the first place, but the best of us can still get stuck. Don't let it put you off enjoying the beach or the desert; just carry the right gear and make sure you know what to do, and you'll be able to get moving again.