Gravity is what allows a the small initial fluctuations in the matter density after the "big bang" (or after exiting inflation), to grow into large differences. Dark matter, followed by baryonic (ordinary) matter, clump, thanks to gravity, into various filaments, galaxies, and in the small(er) scale, stars. Our Sun is one of these stars which gravity clumped together from a cloud of dilute matter, mostly hydrogen. As gravity continues to act, at some point the hydrogen density in the cloud exceeded some density where fusion begins to happen - and the Sun "turned on".
There's a popular misconception that the Sun needed to reach extremely high densities and temperatures to guarantee fusion. This is not true - the Sun is not a thermonuclear bomb, and does not fuse all its hydrogen in one big explosion. Instead, the Sun is well, well, below the temperature needed to sustain fusion, and the reason why a bit of fusion is happening is quantum tunneling - even though the protons (hydrogen nuclei) in the sun do not have enough energy to fuse, they sometimes (with very low probability) "loan" the missing energy, fuse, and "return" the loan from the huge amount of energy produced in the fusion reaction. The rest of the energy heats up the Sun and causes it to shine as we know it.
The energy produced by fusion is also what generates the pressure which prevents the Sun from collapsing further with gravity. When the fusion processes in the Sun will eventually die out, this pressure will disappear, and the sun will collapse into a so-called "white dwarf" (our Sun isn't massive enough to collapse into a neutron star or a black hole).