Do lone pairs contribute towards the pi electron count of heterocycles?
Only count the lone pairs/ pi-bonds/ groups which are participating in conjugation and ignore them in all other cases.

For example, in compound 2 (thiophene), there are two lone pairs on sulfur.



One lone pair (brown) is in a p-orbital, and hence participates in conjugation with the two π-bonds. The other lone pair (blue) is pointing outwards from the ring in an $\mathrm{sp^2}$ orbital. This lone pair is orthogonal, or perpendicular, to the π-system and hence cannot take part in conjugation. In total there are six π electrons, and the compound is therefore aromatic.

See also: Conjugated system on Wikipedia.