In single player, to perform a stealth kill you must be:
1.) Crouched, walking, or still. Running or sprinting will prevent a stealth kill.
2.) Be holding the targeting key and be facing the target. Your character may be facing any direction, but your camera must face the target, allowing things like facing away and turning and stealth killing someone in range instantly.
3.) Be no less than 90 degrees off of directly behind your target. You can knife from the side, as long as you are slightly behind.
To be vulnerable, the target must be still or walking. The target may be firing, but being in a melee stance will prevent a stealth kill.