You’ve gotten a lot of great answers about the technology, but also interesting to consider is that despite how it looks from the ground, the drones are almost never “just a few feet apart”. Much like air shows where planes look like they’re about to touch, there’s quite a bit of distance between each craft. The distance between you and the swarm also means that if individual drones drift a foot or two due to sensor issues or wind, you’re not going to notice it from your vantage point so far away. Think of it like a billboard or a jumbotron screen at a sports arena. From 600 feet away, it looks like perfect resolution, but if you are to walk up close, the pixels are giant and chunky and a lot further apart than you thought. Drones in a warm show are very similar.
From there though, the other answers take over on how they maintain their position and coordination. GPS, base stations with a fixed registered position, and accelerometers and altimeters on the drones themselves.
Latest Answers