On a pvp server:
From level 1-20, you are in your own realms territory. No one can attack you unless your pvp flag is on. This is turned on if you have recently been in contested territory, attacked an enemy realm or healed/buffed a realm-mate who is in combat with the enemy realm. So, basically you're safe in your homelands.
From level 20 onwards, you are in "Contested Zones" which automatically flags you for pvp. So, you can attack or be attacked at any time by members of the enemy realm.
If you're in an enemy controlled zone you may be attacked at any time, but may only attack enemies who are pvp flagged (shown by their red names).