It requires both the strength and the will to oppose death and remain, manifesting oneself in the Force. It's not something one can simply "learn" nor is it something one can master; they either accomplish it or they do not.
Most simply fade into obscurity and never return since they are either content with their oneness with the Force in the Netherworld (Heaven, in a sense), or lacked the inner strength or will to remain in the world. Since most people who achieve that kind of enlightenment are masters with years of wisdom and refinement, they oft make ghosts, whereas younger Force sensitives generally lack those traits and thus do not.
For Sith, it's usually a different ordeal altogether. Sith - especially those strong in the Dark Side - can manifest that energy to bind themselves to the physical world, or to objects containing their essence. This allows them to either possess the body of another who stumbles upon them, or hold out for long enough to be cloned and transferred into said clone. As with all Dark Side immersions, doing so is usually not without consequence, however.