What's the safest way to construct minecart tracks so that they can be used during night-time without mobs ever getting on the tracks and stopping you?
On my server, we always built either a subway or a monorail.
EDIT: Of course, as many pointed out in the comment, all of these solutions need to be well lit, following robust lighting rules, in order to be safe from monster spawning.
Subways are the safest and get you lots of resources. You could build it well under level 20 so you could have a chance at diamonds on the way. It can also give you access to cave systems, abandoned mining shafts and even strongholds. However, it requires to have a good amount of stairways and you could encounter lava lake. But since it's one block at a time, it's easy to get around and patch up a safe zone.
Monorail on the other hand, gets you a nice view over your towns and landscape. A problem would be that you have to build a lot of station upward so you can get off safely without jumping down. It will be impossible for a mob to get up there if your stations are well defended.
Note on normal railways. They are hard to defend. Putting fence around it may prevent certain mob to get to the rail itself, but skeletons and spider can have an easier time to get a shot at you. Completely surrounding it with walls may get you the same result at a subway in the end. Something to consider.
My previous strategy I'll call 'sunken monorail' and I post here just for completeness in the methods listed as answers.
I placed a railway on the ground (or, my second attempt, I dug a dead flat line through hills or built up as required). Then I dug out 2-deep channels on either side to prevent anything jumping up to the tracks.
These were powered tracks so I had redstone torches fairly regularly just over the powered track segments, and put normal torches on the redstone torch blocks to prevent mobs spawning on them. I don't think my light coverage was quite good enough over the whole track length but had few issues with mobs spawning on the tracks.
Worked fairly well; entry and exit points at ground level, reasonably safe. Issues though were occasional spawned mobs on the tracks, skeleton arrows would get you sometimes, and it was pretty ugly in a way as you really disfigured the landscape. Kind of impressive in a major engineering project way with the huge trenches, but also pretty scarring for the views.
Make sure the tracks are well lit so that mobs won't spawn on them, and put fences or other barriers either side to prevent mobs from walking onto the tracks.
You may consider completely encasing the tracks if you wish to prevent skeleton arrows and spiders.