It's called VSEPR theory. In H2S, the valence electrons form 4 groups: two H-S bond and two lone pairs (total of 8 valence electrons about S). The four groups of electrons repel each other, thus the optimal geometry is a tetrahedron with S at the center. However, since lone-pair electrons are 'invisible', we only see two vertices of the tetrahedron, thus it is 'bent'.
in CO2, the valence electrons form 2 groups: two C=O bonds (total of 8 valence electrons about C). The two groups repel to a linear shape.
in CH2O (formaldehyde), the valence electrons form 3 groups: C=O bond (4 electrons), two C-H bonds (Total of 8 valence electrons about C), the three groups repel to the optimal geometry of a planar triagle.
etc.