I'd like to start by saying that, at least up until 1942, neither of the two openly advocated racism against any ethnic or religious group. Nor do we find racism to be a salient feature in the society of the two countries at the time. Rather, much of the horrendous acts of World War 2 were done out of a mentality shaped by bigotry.
It was generally believed after the Great War, that the world was done with all the truculence and bloodshed. What would thus strike one as odd is how two countries (let's leave Italy out of the picture, as your question doesn't concern it) like Germany and Japan could possibly have come together. They made for the most anomalous of allies.
So while the world acknowledged what would probably have been described at the time as 'imperial tendencies' of both Japan and Germany, they were also fairly certain that the two nations would think twice before stirring up any trouble.
Here I'd like to make a passage from The Winds Of War by Herman Wouk available for your reading. It'll paint a more realistic picture of Germany and its people than the one that usually pops into your head when you hear the words 'total war'
Yes, here the Germans sit at the heart of Europe, these perplexing first cousins
of ours, simmering and grumbling away, and every now and then they spill over in
in all directions, with a hideous roar. Out they pour from these lovely little
towns, these fairy-tale landscapes, these clean handsome cities--out they bubble, I
say, these polite blue-eyed music lovers, ravening for blood. It gets a bit
unnerving.
The Germans were genteel people, who were probably trying hard to come to terms with the humiliating conditions set by The Treaty of Versailles. Now let's get down to the dynamics.
Japan and Germany had been working closely with each other for a while, but they cemented their relationship with the Tripartite Pact of September 1940. This sent a strong message to the Allies, who were mustering support at the time.
What historians find particularly fascinating about the Axis Powers is how each of them went independently about their imperial goals, only occasionally lending out a hand to each other.
The political climate in Europe was very nebulous at the time and relationships between countries was hardly ever water-tight. Therefore, the 'enemy of my enemy is a friend' line of reasoning couldn't have played as pivotal a role as most of the answers have made it out to be.
As this video suggests, Japan became relevant in the international scene only after World War 1. Their expansions in the Pacific, as they correctly recognised, had limits. This because of the naval power held by U.S.A and Britain. Less than 35% of the Japanese territory, at that time, must've been arable (at the moment, it's a meagre 15%, with the percentage still falling). Japan also does not have a good stock of natural resources.
This is what made Japan join the war. They wanted to extend their territory and make itself into an imperial power. It's important to mention that they were successful in doing so, by seizing a good part of Southeast Asia and the Western Pacific. This should make much more sense when seen in the light of some of the country's actions in the early parts of the decade. A suitable example would be that of the Japanese invasion of Manchuria, which was done in a bid to increase industrial might.
In the case of Germany, people were aroused to action by Hitler, who claimed to be a nationalist seeking to unify what he felt was German territory. He also promised to make amends by solidifying Germany and giving it back its former glory.

When the initial conquests under Hitler, like the Anschluss that sought to unify Germany and Austria, garnered plenty of support from civilians on both sides of the border, the movement grew to an unprecedented scale.
There was, therefore, very little in the way of ideology that played a role in Japan and Germany fighting together. It is, as you rightly point out, a union based on self-interest and personal gains.