First-principles theory of scanning tunneling microscopy