The Ainu are actually an indigenous population of northern Japan, today mostly living on the island of Hokkaido. They were there before the concept of Japan existed, before there's any written records of the Japanese archipelago.
There are a lot of complications in trying to understand the history of the Ainu. A very popular theory about the Ainu is that they are descendants of the Jōmon people: the name given to the hunting-gathering culture in the Japanese archipelago from around 14,000 to 300 BC. Later immigrants to the islands, the Yayoi, were supposed to have pushed the Jōmon people north. In this scenario, the Yayoi settlers are the ancestors of the ethnically “Japanese” population, while the Ainu are descended from the Jōmon.
Or are these two groups so neatly separated? Genetic evidence now shows that rather than one population entirely displacing the other, newcomers to the Archipelago interacted and mixed with the people already there. A lot of genetic history research is going on in Japan, trying to figure out proportions of “Jōmon” and “Yayoi” ancestry in the current Japanese population, how closely the Ainu are related to other populations in Japan, and to populations in mainland Asia and North America. This research is moving pretty fast, so I hesitate to try to establish any answers here, but if you're interested in researching this further, the following article would be a good place to start: