Minecraft wiki entry for Rail, associated text: If there are no other rails adjacent, or if placed beside an existing stretch of track of any type, then in Bedrock Edition the new rail orients itself as a straight north-south track, and in Java Edition the new rail orients itself in the direction the player is facing.
A new rail placed at the end of an existing stretch of track continues the existing track in the same direction, either east-west or north-south.
If there are two adjacent rails on its level, or one level up or down, the newly placed rail configures itself as straight or curved as needed to connect the other two.
If placed between three adjacent rails (forming a T-junction) the newly placed rail configures itself as curved to join two of the sides.