Magnetic devices like floppy disks, hard disks and some tape units all use the same underlying technology to achieve this. The media (meaning the disk as opposed to the drive) is coated with magnetic particles a few millionths of an inch thick which the drive divides into microscopic areas called domains. Each domain acts like a tiny magnet, with north and south poles, and represents either zero or one depending on which way it is pointing.
Information is read from, or written to, the media using a "head" which acts in a similar way to a record player stylus or the tape head in a cassette recorder. On a hard drive this head actually floats on a cushion of air created by the spinning of the disk. This contributes to the superior reliability of a hard drive compared to a floppy drive, where the head touches the surface of the disk. Both these drives are known as "random access" devices because of the way in which the information is organised into concentric circles on the surface of the disks. This allows the head to go to any part of the disk and retrieve or store information quickly.
Hard disks, in common with magnetic tape media, have the recording layer on top of a substrate. This architecture - referred to as air incident recording - works well in a sealed non-removable system like hard disks and allows the head to fly in very close proximity to the recording surface. A very thin coating, in the range of a few manometers, covers the recording surface to provide lubrication in the event of a momentary impact by the head.