Dear Student,
Molecules are formed when atoms of same element or different elements combine chemically in definite proportions.
When atoms of same element combine, molecule of that element is formed.
And when atoms of different elements combine in definite proportions, molecule of compound is formed.
Regards