The Cornered Cat has an excellent section on dryfiring safely.
http://www.corneredcat.com/article/p...y-fire-safety/
In terms of not learning bad habits, the key is trying to avoid doing things that teach bad habits--as much as possible.
So, for example, although I have some firearms that would require cycling the slide or cocking the hammer for each shot, I tend to do my dryfire practice with guns that have a double-action trigger so I don't have to cock the hammer or cycle the slide.
When I dryfire, I make a point of focusing on my stance and keeping my grip proper so I'm also practicing those things in addition to working on my trigger technique.