The body doesn't notice a lack of oxygen. Hence the danger of carbon monoxide poisoning or air without o2 etc, as you won't notice o2 lacking. What you notice is co2 buildup, and as long as you keep breathing that's not happening.
When freediving, you can't really avoid it. What you learn however is how to deal with it. Control your diaphragm when it wants to start breathing, as the spasms are wasted energy. It's mostly a mind thing. With simple exercises (co2 tables) and just getting used to the feeling, it took my quite a short time to reach 5 minutes.
One "trick" btw is hyper ventilating. But DONT DO IT! It get rids of lots of co2 in your blood stream / lungs, so it takes a bit longer for the co2 buildup. But you need that buildup. Even though it's painful, that's your only signal as to how you're doing and which you kinda calibrate against. Especially when diving, hyper ventilating before can make it so you suddenly go unconscious before you felt the urge to surface.
Wow, TIL hyperventilating increases the danger! My brothers and I used to compete against each other in swimming pools, and we'd always hyperventilate at the beginning, thinking this 'got the oxygen in'. In any case, it definitely helped. Glad we never got into trouble this way.
My personal record is ~3:30, but I'm pretty sure I could go well past that if we had practiced instead of just competing.
You can still do it, just never do it alone
We used to hyperventilate before underwater training to extent our time under water
Actually knew someone who didn't make it, after (presumably) using the hyperventilation technique. Such a senseless way to go.