In the course of evolution, fishes, amphibians and mammals including humans developed 2, 3 and 4 chambered heart respectively. One reason for it could be that due to changing environmental conditions and to support the evolved body structure, these organisms developed the multiple chambered heart to adapt successfully with the course of evolution.
Fishes have 2 chambered heart one ventricle and one atrium while the amphibians have 3 chambered heart; consisting of two atria and one ventricle. They are cold blooded animals and do not need much energy to maintain their body temperature like warm blooded animals (having four chambered heart). Thus, fishes and amphibians can allow partial mixing of oxygenated and deoxygenated blood in the ventricle.
Amphibians also absorb some amount of oxygen through their skin so this supplements the regular respiration. This supplement supply of oxygen through the skin overcomes the absence of two ventricles.
The mammals including humans are warm blooded, they need constant energy supply to maintain their body temperature. Thus, large and constant supply of oxygen is needed to produce energy, so they cannot allow mixing of oxygenated and deoxygenated blood. Hence, they have four chambered heart two atria and two ventricles that prevent mixing of blood..