On the PvP servers if you are in an area controlled by your faction, you can attack any member of an opposing faction if you see them (and they cannot attack you until you attack them first). Contested zones you can attack or be attacked at will. Zones controlled by opposing faction, you can be attacked and have to wait until you are attacked before you can defend. This is what ive heard as to how it works anyways.
In PvE you either have to type /pvp to flag yourself for PvP. Attack an NPC belonging to the opposing faction. Attack a member of the opposing faction who is flagged for pvp. Or if you heal someone who is flagged for pvp, you will get flagged.
Flagging lasts for 5 minutes, if no further actions are taken (being attacked, attacking, or healing a flagged PC) you will become immune to PvP combat.