If you're referring to the very first customers, they usually come from people we know, who then recommend the product to others in their network, and so on :)
If you're referring to random strangers online, I did it by writing tutorial articles on Medium or Hackernoon about the problem I was trying to solve and providing initial guidance on how they could do it themselves. Only then would I subtly promote my project at the end of the article, explaining how it could save them time by using the project directly.
My first few customers were friends and family who believed in what I was building. They helped spread the word to their networks. Also got some early traction by posting on relevant forums and communities to get the product in front of people who might need it. Took a lot of manual outreach and hustle in the beginning, but it paid off in getting those crucial early adopters!