Ride them into the stable, turn so that they're almost sideways inside (you have to do this to dismount).
They've stayed inside for me, as long as they aren't being attacked.
Edit:
Unfortunately, if you fast travel, they still come with you.
If you have Dawnguard, park your horse in the stable, walk off and go summon Arvak and ride him, your horse will stay in the stable permanently. One of my characters had Shadowmere and another horse he owns at Riften stables for ages. Shadowmere did not like sharing a stable at one of the new houses though, so it seems the new horse there might be moved to another house. They could have made the stables a bit bigger.