Police officers, sheriffs, and even park rangers typically enforce criminal trespass law. But there are a few situations in which trespassing charges may be dropped against a defendant.
Here are a few common defenses to trespassing:
Consent. If the alleged trespasser obtained consent to enter the property or use the chattel, then the trespass was legal. Consent can be given through words, actions, or written permission (for example, a license). The property owner's silence or inaction may also count, if a reasonable person would have spoken up. But the consent isn't valid if you obtained it through fraud (namely, by tricking or coercing the owner). You also can't get valid consent from children, people who aren't legally competent, and folks who are intoxicated.
Reclaiming your own property. Under certain circumstances, you're allowed to trespass if you're in the process of recovering property or chattel that rightfully belongs to you. The initial deprivation of your property must either have been the property/chattel owner's fault or an "act of God" such as a storm or wind.
Public necessity. A complete defense exists when you have to commit a trespass in order to protect the public during an emergency. There must be an immediate necessity for the trespass and you must have trespassed in genuine good faith that it was to protect public safety. You lose the protection of this complete defense when your trespass becomes unreasonable under the circumstances.
Private necessity. Although not a complete defense, private necessity lets you trespass if it's to protect someone (including yourself) from death or serious bodily injury or to protect any land or chattel from serious destruction or injury (if they're animals). Though not guilty of trespass in a private necessity situation, you could still be held civilly liable for any damages that you cause during your trespass -- for example, damage to a property owner's fence if you swerved onto his property to avoid a crash.