The first stars were formed about 200 million years after the Big Bang.
The matters in the universe like hydrogen, helium, and lithium condensed due to gravity, eventually to the point where stars began to form.
Large amounts of matter collapsed to form galaxies. The first galaxy was formed when the the universe was around 500 million years old. Gravity eventually pulled galaxies closer and closer towards each other to form groups, clusters, and superclusters.