As I understand it Stalin was pretty busy capturing Berlin before western allies but at the same time finnish front had almost collapsed although the resistance was bitter(for example battle of Tali-Ihantala). Why did they just settle for some pieces of land and Porkkala harbor that they rented?
The short answer is because the Finnish defences held, although it was a very close call.
When the Soviet assault seemed to be on a verge of a breakthrough right after the capture of Viipuri, the Soviet foreign ministry issued peace terms to Finland that would've amounted to unconditional surrender and occupation. After the assault was stopped at Tali-Ihantala and elsewhere, these demands were quickly forgotten. Finland had been seeking a way out from the war from early 1944 onwards, but only after the failure of Red Army to reach a solution did the terms offered become acceptable to the Finns.
Soviets were not prone to reinforcing failures, and while they would've been able to crack Finnish defences with more troops, this would've meant weakening their main effort against Germany, possibly resulting to the Western allies reaching Berlin first.
Post-war, Stalin's hand was likely stayed both by the likely costs of occupation and by the very strong probability of Sweden joining the NATO if Finland became communist. The Soviets were clear that they'd offer military aid to Finnish communists if, but only if, they could take over the government by themselves: they wouldn't get the Red Army to do the job for them. Fortunately, the Finnish communists were too weak and divided to mount a coup.
There's a previous thread about this question in e.g. here:
https://www.reddit.com/r/AskHistorians/comments/58d0pu/how_did_finland_manage_to_avoid_becoming_a/