Many of today's electronic devices require magnets to function. This reliance on magnets is relatively recent, primarily because most modern devices require magnets that are stronger than the ones found in nature. Lodestone, a form of magnetite, is the strongest naturally-occurring magnet. It can attract small objects, like paper clips and staples.
By the 12th century, people had discovered that they could use lodestone to magnetize pieces of iron, creating a compass. Repeatedly rubbing lodestone along an iron needle in one direction magnetized the needle. It would then align itself in a north-south direction when suspended. Eventually, scientist William Gilbert explained that this north-south alignment of magnetized needles was due to the Earth behaving like an enormous magnet with north and south poles.
A compass needle isn't nearly as strong as many of the permanent magnets used today. But the physical process that magnetizes compass needles and chunks of neodymium alloy is essentially the same. It relies on microscopic regions known as magnetic domains, which are part of the physical structure of ferromagnetic materials, like iron, cobalt and nickel. Each domain is essentially a tiny, self-contained magnet with a north and south pole. In an unmagnetized ferromagnetic material, each of the north poles points in a random direction. Magnetic domains that are oriented in opposite directions cancel one another out, so the material does not produce a net magnetic field.
In magnets, on the other hand, most or all of the magnetic domains point in the same direction. Rather than canceling one another out, the microscopic magnetic fields combine to create one large magnetic field. The more domains point in the same direction, the stronger the overall field. Each domain's magnetic field extends from its north pole into the south pole of the domain ahead of it.
This explains why breaking a magnet in half creates two smaller magnets with north and south poles. It also explains why opposite poles attract -- the field lines leave the north pole of one magnet and naturally enter the south pole of another, essentially creating one larger magnet. Like poles repel each other because their lines of force are traveling in opposite directions, clashing with each other rather than moving together.