The bombing of Japan was a warning to the USSR. The allies knew that Japan would surrender at the drop of a hat without a fight as the had actually asked to be allowed to surrender ten times before the first bomb was dropped.
The official reason for denying them a surrender was that they had placed various requirements on the surrender but in actual fact almost all of these demands were actually accepted in the end - the biggest request from the Japanese PoV being, of course, the continuance of the Emperor's position which was granted.
There was never any chance of a fighting invasion of the Japanese mainland, and everyone knew that well in advance. That idea is simply propaganda. The Japanese were beaten and they knew it. If there was the odd general who wanted to fight on the simple fact was that he would have had to do it himself as the army was on the verge of mutiny, as was the remains of the air force. Again, the vision of the unconquerable samurai who would die before surrendering is a comfortable myth bolstered by a handful of freaks like Hiroo Onoda. Such imagery is no more a true picture of the Japanese army than the Alamo is of the US army en mass.
When MacArthur presented Japan's documentation on the subject of being allowed to surrender, Truman reportedly dismissed the idea without even reading the proposal, commenting that MacArthur was a great general but a lousy politician - a strong clue that the bombing was a political event rather than a military one.
The reason for the second bomb has been debated but is likely to be a combination of two main reasons: firstly to test the second device's design which was substantially different from the first; secondly to hint to Stalin that the US had a supply of these things, not just one made through some super-human effort which would be hard to replicate quickly.
It is easy to forget the degree to which the Japanese had been dehumanised in the US. The idea that a bomb design be tested by being dropped on civilians would not have raised anything like enough of a protest in the US if it had been floated beforehand and, indeed, continued to be regarded as completely justified for decades afterwards in general public opinion.
The ultimate roots of the bombing are a fascinating story of the interaction of militarism and religion on both sides, going back to the days (less than a century before) of Admiral Perry's expedition to "open" (ie, threaten to bombard into submission) Japan and the reaction of the Shogunate to that challenge. In light of that, there is a horrible irony in the fact that Nagasaki was bombed (due to weather), as it was one of the first cities opened to the outside world and was opened specifically as an attempt to avoid Japan being attacked and conquered by the US.