As you know, halogens are located in group 17 of the periodic table, which means that every halogen has 7 electrons in its valence shell.
With a couple of exceptions (mainly hydrogen and helium, which have a duet), atoms "strive" to achieve a complete octet because this electron configuration, i.e. 8 electrons in the valence shell, is extremely stable.
As you would imagine, having 7 electrons in the valence makes halogen atoms extremely reactive. These atoms are "desperate" to gain one more electron and achieve a complete octet.
The easiest way for a halogen atom to achieve a stable electron configuration is to share one electron with another similar atom.
As a result, a diatomic molecule is formed and both halogen atoms now have a complete octet.