A dog's sense of direction is impressive but not infallible.
Dogs rely primarily on scent, using their highly developed noses to track trails over long distances. They also use visual landmarks and terrain features. Some research suggests magnetoreception may play a role, but this is not fully proven.
Navigational skill varies by individual, breed, and experience. Dogs are reliable in familiar areas but less so in completely unfamiliar territory. Always use leashes and precautions in new environments.