Popping out of gear means wear to whatever holds it in gear. Check your indent balls and springs, indent ramps on the shift rods, shift forks and the splines on the gear and synchro-hub "slider". If it jumps out very occasionally, you might be able to repair it inexpensively. If it has been jumping out for a long time, then you'll need to change expensive, hard-to-find bits like the synchro-hub and/or the gear itself.
What causes it in the first place? Usually not be able to engage the gear fully and properly. Often this is caused by something blocking the linkage, misalignment, etc. With the gear not fully engaged, it can vibrate out. This often wears a tapered ramp in the gear and sychro-hub "slider" splines.
The key is to catch it before the damage starts. Always make sure you have full engagement. Do not ignore isolated incidents. Find and fix the problem before it gets serious.