How Nether portals work is a mystery to a lot of people. Sometimes they bring you to the right exit, but sometimes they bring you to an entirely different place!
Fortunately we have the /nethercoords command that tells you exactly where to place your portal in the other world, based on your current position. For example, if you are in overworld and you are where you want to place a portal, simply type the command, write down the coordinates, go to the Nether, and place the portal at those coordinates.
When you pass through a Nether portal from the overworld, Minecraft divides your X and Z coordinates in half (meaning every step you take in the Nether, you actually walk 8 blocks in the overworld), calculating the "ideal exit location." Now, the reason why you don't end up in the right place all the time is because Minecraft doesn't immediately place you at this ideal exit location; instead, it looks for another Nether portal nearby up to some distance, which might be quite aways away!
If there is no existing portal on the other side, you'd think that Minecraft would create a portal at the ideal exit location, but it actually instead chooses a random location nearby to place the portal. This is not a problem if there's only portal in an area, but if there are two or more portals, those random locations mean that the first Nether portal created might actually later be closer to the second overworld portal created!
The only way to fix this mixup is to make sure the portal on the other side is as near as possible to the ideal exit location, so the nearest portal on the other side is always the one that you want. The /nethercoords command calculates this ideal exit location for you (to be placed on the other side).