Can only gases by diatomic?
Any molecule composed of exactly two atoms is diatomic, but I will answer this question assuming that you mean elemental diatomic molecules. Only H, N, O, F and Cl are diatomic elemental gases under STP. Br and I are diatomic liquid and solid respectively, under STP. Oxygen remains diatomic in liquid state, while nitrogen and all halogens(F, Cl, Br, I) are diatomic in all three classical physical states. Gaseous sulfur can be diatomic at or below standard pressure. Structurally similar to diatomic nitrogen, diphosphorus is an unstable allotrope of phosphorus. Diatomic carbon is a gas that exists under very high temperature. There may also be other diatomic allotropes.
Some elements form stable diatomic molecules because of the octet rule (or duplet for hydrogen). Generally a bond can share up to three pairs of electrons, so elements which are three or less electrons short of a stable electron configuration can pair up and share up to three electrons to become stable. Beyond that, elements will find other ways to become stable.
However, chemical stability isn't governed only by the octet rule, there are other factors such as geometry, bond length and nucleophilicity/electrophilicity that affect molecular stability. Therefore, some diatomic allotropes are not the most stable allotropes of the elements. Examples from above are disulfur, diphosphorus and dicarbon.