As someone mentioned earlier, it's all about when the swing starts. Backstabbing on TF2 isn't always about the knife hitting them in the back, it's a separate kind of attack. A sort of alternate fire. If you click while looking at someone's back, you'll start the backstab attack, which has a completely different animation (downward swing with the knife held pointing down) and timing. If they turn around during this, the "backstab" still comes down and hits them.
However, just because you do a "normal" attack doesn't mean you won't backstab. A normal attack (sideways swipe) actually hitting the back has the same instant-kill effect.
I've actually experimented with this on people, and if you really wanted to, you could get interesting effects out of this. Say you're in a sniper nest, about to stab a sniper in the back, and a soldier turns the corner and comes at you. Start your swing at the sniper's back, and during the longer animation for the backstab, turn toward the soldier. Then repeat on the sniper.
...Would this be against server rules, actually? It's not really a glitch, I don't think, and actually would take a bit of skill to pull off.