The ancients learned to make a compass and use it to find north, but they could never have figured out why it worked.
For that, we can thank English scientist William Gilbert. He was the first to describe the Earth itself as a magnet in his important work De Magnete, published in 1600. The rest is modern science.