Residence rules for students and expats

Which parts of the law depend on living in Germany and what the law means by residence and habitual abode.