The wording thus far has been that the Dog cannot die- for story reasons.
Now. Let's look at it this way.
A dog wanders into a settlement.
No one is really going to notice that dog, unless the dog is rabid, or a complete [censored] head.
So, they can hang out, really anywhere. Indefinitely.
A person? "Hey, why has that guy been standing across the street for a week? It's like he plans to stand there, indefinitely, or something! We need to keep an eye on that guy."
So... We have this dog. It's just hanging out.
Here's you in this bright vault suit, all naive and ignorant of your wildly changed setting. He seems friendly, and you are lonely. (it seems Codsworth's programming doesn't really allow him to process what has occurred).
But the dog is really programmed to wait until someone comes out of a vault and follow them. Data does show, many people in the pre-war world considered the dog to be man's best friend. Maybe, after you've formed a really good "master/dog" relationship, they will attempt to lead you somewhere.
"What's that, Lassie? Timmy's stuck down a well?" Only the well is the Institute, and you wind up being Timmy, after they capture you.