A compass needle seems to know where north is.
It is not reading a map.
It is responding to Earth’s magnetic field.
Earth behaves somewhat like a giant magnet
Earth has a magnetic field that extends far beyond the surface.
The field is generated by processes deep inside the planet, especially the motion of electrically conducting liquid iron in the outer core.
Near the surface, the field provides a general north-south direction.
The compass needle is a magnet
A compass needle is magnetized.
When it is free to rotate, Earth’s field exerts a torque on it.
The needle turns until it lines up with the surrounding magnetic field.
The marked end points roughly north.
That connection makes more sense once you understand why magnets attract iron and respond to magnetic fields.
Magnetic north is not exactly the North Pole
The geographic North Pole is defined by Earth’s rotation axis.
Magnetic north is defined by the magnetic field.
They are not in the same place.
Magnetic north also moves over time.
Why does that matter?
For ordinary navigation, the difference may not matter much.
For precise surveying, aviation, marine navigation, and wilderness navigation, the angle between true north and magnetic north can matter.
That difference is called magnetic declination.
Does a compass point to the North Star?
No.
The North Star happens to lie near the direction of geographic north in the sky.
A compass does not detect stars.
It works in daylight, indoors, or under clouds because it is responding to magnetism.
Can anything interfere with a compass?
Yes.
Nearby magnets, steel objects, electrical equipment, vehicles, and magnetic minerals can distort the local field.
A compass placed next to a strong magnet is going to care far more about the magnet than your hiking plans.
So the compass is not reading the map.
Earth itself is providing the magnetic instruction.
And that’s the short explanation.
Related explanations: Why do magnets attract iron?, Why do birds migrate?, and Why do stars twinkle?