Before, scientists used to simulate this behavior and then compare it to what occurs with birds in real life in an attempt to demonstrate the how and the why. However, now computer vision can measure both position and velocity of objects in a frame, thanks to the work of William Bialek at Princeton University, which is demonstrating that birds are capable of matching the speed and direction of their neighbor birds.
Additionally, the concept of “critical point” helps explain this, showing that the social desires of the birds overwhelms the motivation of each individual bird, as they work toward flying as a collective flock and not as solo birds.
There still remains more to be seen and explored, but check out this study for further reading.