Good question! It's a fun one. There are a couple of physical changes in their skeletal structure that allow for such flexibility. 1) Their cervical vertebrae are really long, 2) they have a different type of joint between the cervical vertebrae than other quadrupeds, and 3) their first thoracic vertebrae has evolved over time to function more like an 8th cervical. I love this topic, but we're gonna have to get into some anatomy basics to really do it justice. CW for photos of skeletons / bones below here.
So, first off, let's talk about cervical vertebrae. These are the "neck" vertebrae that go from the base of the skull to right before the first rib (the vertebrae that articulate with the ribs are called "thoracic").
Basically every mammal has the same number of cervical vertebrae - seven. (The fact that this is so steady across mammalian taxa is an example of what's called "evolutionary conservation, where traits stay steady across time and lots of other changes). Since species aren't changing the number of cervical vertebrae they have, changes to their shape and size happen instead. Giraffe are a perfect example of that.
Giraffe neck bones are super long. Compared to other mammals, they haven't just been "scaled up" with even changes to the proportions - they're actually very elongated.
Next, these cervical vertebrae actually articulate (a fancy word for forming a joint) with each other differently than the spines of other species. The pieces of each vertebra in the cervical spine of most mammals overlap in a way that allows movement in some directions, but not others. You can try this yourself: turn your head to look over your shoulders, and then nod your head as far forward and then back as you can. As a human, you'll have much more range of motion going forward-and-back movement than side-to-side. That’s because when you want to turn your head, each vertebra can only move so far in that direction, so you have to turn all of them a little bit each to get a real “turn.” But when you want to nod your head, each vertebra can move a lot farther in that direction, so you can go a lot farther. The degree of restriction on movement in the cervical spine varies by species, and gets really technical, so I'm not going to go into it here - but here's an image of the cervical spine a horse to give you a sense of what those overlaps normally look like on an animal with a (relatively) long neck.
Now, giraffe aren't closely related to horses - or really many other non-extinct species - so comparative anatomy research for giraffe frequently compares their physical structure to that of their closest living relative, the okapi. So here's a diagram showing what giraffe and okapi cervical spines look like when compared. Giraffe on top, okapi below. We're going to be talking about the middle five bones for now, which are cervical vertebrae 3-7. (C1 and C2 are oddly shaped because they have to interact directly with the skull, and the two on the far right of each spine with the long bits pointing up are T1 and T2, the first thoracic vertebrae).
You can see that there are some pretty major differences between the two cervical spines, even though giraffe and okapi are fairly closely related! The okapi vertebrae are shorter and squatter and still have a lot of that large overlap with each other. They also all look a little bit different from each other. Where as the giraffe vertebrae don't have a ton of overlap and look really similar to each other for the most part! This, my friends, is where we get into the secrets of giraffe necks.
Joints that need to have a ton of motion in lots of directions are built differently. In the human body, two good examples are the shoulder and the hip. Those are what we call ball-and-socket joints: the bone ends in a fairly round protrusion, which can then rotate within the joint in lots of different ways without physically running into another bone.
Guess what! Giraffe cervical vertebrae also have what are effectively ball-and-socket joints! Take a look at this image from Bones Clones of a disarticulated giraffe spine, which shows it really nice and clearly.
See those rounded bumps on the top of each bone? That's the surface that articulates with the underside of the one above it. Because there’s so little overlap between the parts of the vertebrae the way there is in other mammals, that ball-and-socket type articulation gives giraffe a lot of range of motion at each individual joint in their necks. This is a really unique trait to giraffe - okapi have a similar rounded joint surface, but motion is still much more restricted by other parts of the bone. Let’s take a look at a comparison in the image below. This time okapi (A) is on top and giraffe (B) is on the bottom.
The surface we’re looking at is that rounded part labeled (1). You can see how in the giraffe (B) there’s not a lot of bone around it, which means there’s more room for each bone to rotate; but on (A) the okapi, while there is that rounded surface, the parts of the bone labeled (2) and (3) stick out even with (1) and will eventually get in the way of motion.
Giraffe still have one more evolutionary trick up their sleeves that adds to their neck flexibility. Remember how I said almost all mammals have seven cervical vertebrae? And that thoracic vertebrae are defined by being the ones that connect to the ribs? Well... the first thoracic vertebrae in giraffe looks really weird. To the point that there was even a period of scientific inquiry into if they actually had ended up with eight cervicals! Spoiler: Nope, but their T1 is heavily modified, and while it still connects to the first rib, it also kind functions like a cervical vertebra!
So here’s the thing. The thoracic spine in mammals is pretty rigid because that’s what supports breathing. If your ribs (and the things they connect to) aren’t rigid, they aren’t stable enough for your muscles to pull on them, which means your diaphragm can’t contract and create a vacuum that pulls air into your lungs, etc. Movement in this part of the body makes breathing much more challenging, or even impossible. That means that it’s really unusual for a thoracic vertebrae to have much of a range of motion at all. But for giraffes, T1 does! Let’s look at this image below, from a paper looking at the comparative anatomy of C7/T1 in giraffe and okapi. The grey shading indicates a high amount of inter-vertebral flexibility, and white indicates an inflexible area.
Notice how on the okapi at the bottom, the spiky parts going up (called spinous processes, which are muscle attachment sites) are all parallel and shown in white to be inflexible? But for giraffe, T1′s spinous process isn’t parallel with T2 and T3′s, and it’s shown in grey to indicate - which means that it's actually able to move with the cervical spine when the deep neck flexor muscle (indicated in red) engages. That’s unheard of! Thoracic vertebra don’t do that!! Except, apparently, in giraffe. Here’s a short video clip showing what that looks like when T1 and T2 articulate. Okapi is on the top, giraffe is on the bottom.
Look how much further the giraffe’s spine can move at that T1 joint than the okapi! It’s got a far larger range of motion both vertically and in rotation sideways.
So all of these physiological changes add up to giraffe necks having a larger range of motion and flexibility starting at the very base of the neck, which is then amplified by each joint in the neck also having more range of motion than normal. Combine that with really long neck bones, and you’ve got an animal that can reach in almost any direction!
No, really, literally almost any direction. Here’s a fantastic diagram of some of the different ways giraffe necks can flex, reach, and bend. Notice how in the postures labeled MAX-DF, you’ve got both rotation of the neck backwards towards the hind end, and curvature down! That’s not a thing any other living animal can do!
Fun fact: this last diagram comes from a paper using giraffe neck mechanics as a possible model for the range of motion in sauropod necks.