Proteins are targeted to or from the nucleus by virtue of having nuclear localization signals or nuclear export signals, respectively, allowing the proteins to be recognized by receptors that direct their transport through the nuclear pore complex.
Textbook Reference: The Nuclear Envelope and Traffic between the Nucleus and the Cytoplasm, p. 362