Your choice of stem on a mountain bike determines your riding position, and that in turn has a big effect on the bike's handling.
Cross-country racers tend to use using longer stems (around 120mm) to increase the stretch in their position. Long stems also stabilise the steering of a bike, making it less keen to turn suddenly on tight singletrack but less nervous at high speed.
In contrast, downhill or four-cross riders use short stems (80mm or less) to quicken steering, and to shorten the distance between saddle and handlebar, making it easier to get off the back of the bike. This makes them great for snap direction changes or controlling a sliding front wheel mid-race, but it can make the bike more of a hyperactive handful at high speed.
As little as 10mm on a new stem length can make a big difference in the handling character and cockpit reach balance of your new bike. If possible borrow different length stems from your mates or your local shop so you can try what effect they have before you buy.
If you're getting uncomfortable on longer rides, don't assume that a shorter stem is the necessarily answer. Sometimes a longer stem will better distribute your weight, or move the load on your upper body to stronger muscles.
Rise also has an effect on handling and comfort. The easiest way to experiment with stem height is to leave your fork steerer long and move the stem up and down it, using spacers. This doesn't always look great, but it's a lot cheaper than changing stems.
Stems and handlebars come in two clamp sizes, the 25.4mm and 31.8mm, the latter known as 'oversize' compared to the original, narrower clamp. The thinner clamp size bar/stem combos are generally lighter and their flex is claimed to make them more comfortable on long rides, though materials and design also make a big difference.
Oversize cockpits are generally stiffer and stronger though so they make sense if you want a tougher, more rigid front end. The controls and grip areas of both are 22.2mm in diameter, but you may find some lights and computers don't fit the 31.8mm size, or need special adapters.
Our advice is to stick with whatever size your bike came with, unless you are happy to swap both bar and stem at the same time. The larger size may well end up dominating simply because it looks beefier, and also because it's becoming very common on road bikes. Some manufacturers are recommending the same stem for road and cross-country mountain bike use. That's only possible in 31.8mm because 25.4mm road bike bars are now almost unknown.
Materials & manufacturing
Mountain bike stems are almost always made from aluminium. Cheaper stems are made from less strong grades such as 6061, while lighter, more expensive stems use 2014 or similar. Over the last few years forging techniques have dramatically lightened stems with no widespread increase in reports of failure.
A very few manufacturers are making mountain bike stems from carbon fiber. At the moment, these have to be seen as for special purposes only: saving weight on very high-end cross-country race bikes that are going to get frequently inspected by good mechanics. It's not that a carbon stem is going to be weaker than an aluminium one, but it's easy to damage a carbon part in a crash in a way that leaves it weakened, and that damage is hard to see. The subsequent failure can be catastrophic.
Cheaper bikes occasionally come with welded steel stems, and it's not unusual to see high-quality welded steel stems on older bikes.
The ultimate bling is a titanium stem. There's no significant weight difference between a titanium stem and the best aluminium stems, but titanium is refreshingly expensive. The downside is that titanium can be characteristically "flexy", not a performance attribute you particularly want in a stem. The moral is to shop wisely when considering titanium stems.
Two vs Four bolt clamps
Early mountain bike stems used one or two clamp bolts to hold the bar, which threaded through the clamp. Almost all modern stems use a two- or four-bolt clamp with a removable front cap.
It's debatable whether you need four bolts. Easton makes a good case for two-bolt clamps 2 vs 4