I have a bit of a science fiction scenario. Bear with me for a little bit.
Let's suppose for a bit this approximate series of events was the actual goal of Mr. Varoufakis and company.

Why would they want this and who would want to risk the fate of an entire country and their own reputation on a "gamble"?

Because they started the negotiation game with a huge disadvantage and there was no way that they could bring something new to the table. So Tsipras and Varoufakis chose to "play the dumb communist" scenario that everyone was painting, stretch the canvas a bit and get new offers on the table and start with a different negotiating position.

This hypothesis, which I will check the following days, proposes that the Tsipras government and Varoufakis knew and understood very well the nature of the political and economical forces that pushed on them from outside of Greece. Knowing those forces and playing on the predictions laid out by the other side (Germany was already unhappy after Syriza won, expecting to have a difficult time negotiating with them) they followed to the line the "predicted" course. Difficult negotiations, huge differences in views, big political declarations and discourse, etc. The goal in the end was simple enough: get a cleaner slate.

Both the turbulent negotiations, with Varoufakis playing the evil marxist scapegoat, and the referendum, after a week of bank closures, were used as a ruse to lead the European leaders through to a "clean slate" phase. It was a very risky move, but I believe that the various contradictory signals that Tsipras sent just before the referendum confirm that it was a game. Like poker player that accidentally reveals his weakness just before he pulls a full house. The resignation of Varoufakis today is the next piece in their scenario: just take out the nasty opinionated communist and all the european parties will clearly feel more relaxed. I believe that this move was planned from the beginning and Varoufakis was used to "stir up" the pot a bit, while apparently being discarded as "peace offering" after a while.

So what was gained by this daring gamble?
First of all, the markets were calmed. The negotiations were so desperate from the start, given the position of the two parties that everybody was sure of the Greek default. Now, with the financial markets accepting the default event in itself, any new deals on the table will have to take this fact into consideration. It's not "looming" anymore, it is here and it is indisputable.
Second, the situation now allows for a much needed debt haircut. Refinancing is all fine and dandy, but everybody agreed that the debt could not be sustainably repaid. So now everybody is suddenly ok with the debt reducing.
Third, the Tsipras government kept its word with its electors. They "fought" valiantly for the good of the people. Actually with the cards they got dealt by the previous government, they played quite a good game until now. And their general appeal has definitely risen from 36.3% (plus affiliated parties) to a 61% (as shown by the referendum).
Fourth, the european hard-liners were blind-sided and the rules for the negotiation were changed. The deal before this referendum was "behave or we will strangle you". Now, it's quite a useless threat given the actual default. So Tsipras most certainly will be able to have better terms.

Sorry for the long post, had a bit of thoughts to get out. :D