Ah Stava. Even the mention of that word can bring up debates about suspected poor trail etiquette of its users. Segment speeds are used as fodder for anti-bike advocates. But it's a heck of a good training tool.
You need ideal conditions: Strava leaderboards list people's personal bests. There might be asterisks for weight classes or age groups (for premium users), but it doesn't give a shit if you got your best time against the wind, in the mud, or on a busy night. Road bike segment leaderboards will be packed with people on slick road tires (not mountain bikes), often riding in a group to share the workload, and with a tailwind. So don't go hunting solo, into the wind, on your mountain bike. Conversely for mountain bike trails, you're going to want tacky trail conditions, no fallen trees, and you should ride the trail when it is not busy so you won't have to slow down or stop for other riders using the trail.
Know your segment: It doesn't hurt to preride your segment to find the fastest lines, how to pace yourself, or study online where it starts and finishes. Some segments are poorly made with non intuitive starts and finishes or based off poor GPS traces, but those are the cards you have to deal with. Don't be a loser and make your own segment.
Don't hunt at the very beginning of the ride: Not only are you not warmed up yet, but your GPS likely hasn't had enough time to really lock in on the satellites. You might be able to put in a good time, but it won't count if your GPS trace is out to lunch.
Experiment with entering and exiting trails from different directions: These might be ways to trick your GPS track into giving you a head start or an early finish line. At the very least, you'll find a way that lets you carry speed in and out of the trail. Don't start hunting from the entrance of the trail, or stop right at the end of the trail.
Average wattage: For road segments, you can look at average wattage of your previous attempts to make sure that you don't blow up early or leave too much in the tank.
Be smooth: Some of my best times happened when it felt like I held up a little on the throttle and rode more smooth and controlled. I paced better so I could give it more gas at the end without giving up too much at the beginning.
Don't hunt: Bask in the glory of beating the Strava heroes in a real race! (Mostly) closed course, mano et mano.
Some mountain bike segments are too close to roads and it's hard to compete with road bikes as you weave through singletrack alongside the road. Sometimes the trail has been rerouted, making it almost impossible to beat the previous times.