Why am I sometimes outside of town when I log in?

Solution 1:

You always start from your most recent checkpoint, or, if you selected a new quest step, wherever that quest step starts. It's not a bug.

Solution 2:

As others have said, "Resume Game" will log you into the game at the location of your most recent Checkpoint or Waypoint; depending on where your last Checkpoint or Waypoint was, you may find yourself in a dungeon, somewhere in an overworld zone (this can be particularly dangerous!), or in town.

Normally, it's not a big deal. However, if you want to minimize the chances of anything going wrong (for instance, if you're playing in Hardcore mode and being ambushed by a pack of rares upon login can kill your character), you can choose the "Change Quest" option rather than "Resume Game" -- this will allow you to choose a "safe" checkpoint at which to enter the game that you know is devoid of enemies (that is, a town or monster-less area; randomly-generated areas will be completely rebuilt including monsters, so they are not safe). The beginning of your current quest is often a safe pick, as you will more often than not start each quest in town. It may set you back ever so slightly in terms of progress, but it's an alternative if you're concerned about your safety.

Solution 3:

You save at the most recent checkpoint achieved when questing, assuming you resume a previous game; otherwise, it loads you at the starting point for the quest selected when joining the game.

However, these checkpoints are always safe (unless you are in a multiplayer game and someone trains monsters back to the checkpoint). Monsters will not normally spawn at checkpoints.