How does the electron know which way is spin up or spin down

In the stern gerlach experiment how do the electrons know which axis to point towards, does it use the information of the magnetic field somehow