The connection between thermal and non-thermal emission in gamma-ray bursts: general considerations and GRB 090902B as a case study