The Japanese attacked Pearl Harbor because they wanted to knock the US Navy in the Pacific out of action long enough to allow them to consolidate their empire. When they did, they swung American opinion on WWII. Where Americans had once been reluctant to join the war, they were now eager to do so.
The Japanese wanted an empire in Asia and the Pacific. They felt it was their due as a powerful nation and they also felt that they needed it so they could have a guaranteed source of natural resources. However, they realized that the United States would oppose their attempts to get an empire. This was partly because the US feared a strong Japan and partly because a Japanese empire would have to be taken from America and/or its allies. The Japanese believed that they would be able to keep and hold an empire if only they could destroy American naval power in the Pacific. They calculated that, by the time the US built its strength back up, Japan would be entrenched enough in its empire to be able to hold it. They also figured that the US would not be interested in going to war to regain the Philippines or to help Britain regain Malaya and help the Dutch regain Indonesia.
Before Pearl Harbor, most Americans had been reluctant to enter WWII. They had felt that the war was not their problem. They did not want to get dragged into another European war as had happened in WWI. After the attack, Americans were enraged. They were eager to fight Japan because of the attack and, when Germany declared war on the US as well, they were very willing to fight that country. The Pearl Harbor attack, then, backfired because it failed to knock out American naval power in the Pacific and because it caused Americans to strongly support the idea of getting involved in WWII.