Mountain bike sizing is not an exact science. Compared to road cycling, for example, there are many factors that can influence what size bike riders should choose. Constantly changing terrain means that nothing is constant when mountain biking, so your position on the bike is constantly changing, too.
However, there are some general guidelines and measurements to consider when deciding what size bike to buy. One of the biggest contributing factors that determine bike size is rider height. After all, most mountain bikes are designed to be pedaled in a (mostly) seated position, and a rider's height will largely determine if this position is comfortable, or not.
An important bike measurement related to height is the standover height. This is how far the top tube of the bike is from the ground and can be compared to a rider's inside leg (or inseam). A rider's inside leg should be a larger number than the standover height of the bike. Another way to think of this is that a rider should be able to comfortably stand flat-footed on the ground while straddling the bike. If there isn't enough of a gap between the top tube and the rider's inseam, the bike is too big.
Another measurement related to height is the seat tube length. This determines how high or low the saddle can be. If a tall rider is on a bike that's too small, with too short of a seat tube, the saddle won't be able to go high enough.
Reach is another vitally important measurement on a mountain bike, but one that's a little harder to understand. It refers to the distance of an invisible horizontal line coming from the center of the head tube meets a vertical line emanating from center of the bottom bracket. This is essentially how far the rider is from the handlebars when they are standing up on the bike. A shorter reach will make a bike feel compact, while a longer one will make it feel more roomy.
The effective top tube length (also shortened to top tube length sometimes) is the distance between the head tube and the seat tube. This is how far the rider is from the bars when they are seated.
A taller rider will usually need a bike with a larger top tube and reach numbers, while a smaller ride will usually better suit smaller numbers.
This may all seem confusing, which is why bike companies offer multiple sizes of bikes, which feature different measurements. For comparison, let's take a look at the geometry of the Santa Cruz Megatower, one of the best enduro bikes on the market.
On the Megatower, the reach on a size small is 425mm and the top tube length is 567mm. On a large, the reach is 470mm and the top tube length is 619mm. For the small, the standover height is 702mm, while the large is 713mm.
Bike brands usually recommend a size based on the rider's height. For example, in Santa Cruz's case, a person that's 1.57-1.65m will need a size small, whereas a person who's 1.75-1.85m will ride a large.
The best way to figure out if a bike fits is to visit a bike shop. Try the size that best corresponds with your height, and see if it is comfortable. Bike shop employees are usually trained to assist with this as well.
Riding style can also influence bike fit. Height isn't always perfect, with many people falling in between sizes. For example, according to Specialized's size guide for its Enduro model, a person that's 1.85m could fit on a size S4 or an S5.
This is where geometry numbers like wheelbase come in. The wheelbase is the distance between the front and back wheel. A longer wheelbase leads to more stability, while a shorter wheelbase is more agile and maneuverable.
If a rider wants to race and therefore is looking for stability at speed, they would choose a longer wheelbase bike (S5). If someone wants to do lots of jumps and hits, a shorter wheelbase would be ideal (S4). That's the idea behind Specialized's sizing system. Two riders that are the same height can tailor their bike size to the style of riding they want to do.
Once a size is chosen, there are tons of other ways to improve the fit and comfort of a bike. These are crank length, saddle height, saddle angle, cleat position, and stem length.
For instance, a cross-country racer may want a longer, lower stem for a lower, more aerodynamic position, while an enduro rider will want a shorter stem and higher bars for more stability and control on steep descents.
It's quite easy to go down the rabbit hole on this stuff, and lots come down to personal preference. The best way to find what works for you is to test multiple bikes. Some shops offer test rides or demo events to try out different bike sizes. This way you can see how a particular size actually performs on the trail.