Well. Your question is based on the following image, let's assume.
Woah! Many tracks, right. So, let's understand the mechanism of a train changing tracks on a primary level first. And for starters, Locopilots have nothing to do with Track Switching. They just have to obey the speed limits to ensure a safe journey.
This is a common misconception that this kind of steering wheel directs the train into branches of tracks. This is used for a different purpose which is a question for another time.
This is a simple one. Let's start with this. We'll be coming back to image frequently in this answer for gaining more clarity whenever required.
Some preliminary knowledge is required for understanding the mechanism. This knowledge concerns the wheels of railway units(locomotive, coaches etc.).
Out of the three parts the Flange (the projection like structure out of the wheels) decides the direction of train. The Flanges help the train to travel on curves and also help in the track switching process.
The trajectory of the wheel on curves is the answer for different question and is for another time. For now, let's concentrate on the Track-Switching fun.
Now, in the second image of this question, a track is dividing into two. The track is adjusted for the trains such that they travel on the right branch. This can be explained in a simple way using Flanges of the wheel.
Carefully observe the second image. At the joint, the right side has a small gap. And on the left side, there is none. I'm going to clarify it more with the following image.
See the Yellow circle: The small gap allows the flange of the wheel into it.
See the green rectangle: The emerging track is attached to the source track. As the flange of left wheel, which remains inside of the track, (considering the train is moving into the plane of the picture) has no gap, the attached rail track guides it onto the right branch.
As everything is associated with some technical nomenclature, let's learn that now and we'll discuus the rest of the answer in technical terms while we learn the process of the track switching.
Stock Rails: These tracks/rails don't involve any kind of crossings and can be understood as parent track.
Point: (Earlier referred to as Joint) These are the movable rails which guide the wheels towards either the straight or the diverging track. They are tapered on most switches.
Switch Motor: This motor controls the switch rod between the points. This can be understood like more of gearbox. This is usually controlled by the Station Master. Pointsmen deal with the manual problems related to these points and motor.
Closure Rail: These rails guide the train into a new branch.
Guard Rail: These rails protect the train from derailing of the train. Soon after the train crosses the frog rails, these rails support the wheels to stay on the stock rails that follow.
Frog Rail: This is an X shaped rail intersection at every point.
Now correlate the above image with the second image. From the switch rod POV, the train is moving to the right of the picture and moves into the non-horizontal branch. At the point the right wheel is directed onto the new track. At the frog rails, the flange of the left wheel comes completely onto the new track. (Before the frog rails, the direction of the wheel is altered. The wheel doesn't come onto the new track).
Now again, let's go to the second picture. You'll be able to distinguish all the parts mentioned above.
If in case any stones or any living beings like rats and snakes get stuck in between the point, the pointsman immediately notifies this to the SM and he changes the home signal to Red, and possibly the distant signal to Yellow. So, the trains enter the station safely on the right track after the repairs are done.
The same operation is carried out extensively in case of first image. The station master of such railway station remains alert all the time and maintains the entrance and exit of various trains into and from the station.