When you take the census of Bnei Yisroel according to their numbers, let each one give to Hashem an atonement for his soul, when they are counted; and then there will be no plague among them when they are counted
It is written in the beginning of our parsha (Shemos 30/12), "When you take the census of Bnei Yisroel according to their numbers, let each one give to Hashem an an atonement for his soul, when they are counted; and then there will be no plague among them when they are counted."
The Torah doesn't specify in this possuk exactly what sin Bnei Yisroel did that necessitated atonement. However the Medrash (Medrash Tanchuma siman 4) explains the background of this possuk.
"It is written in Tehillim, (perek 3/ 3-4) 'Many say about my soul' - referring to what the Nations of the World say about Bnei Yisroel, 'Can a Nation that heard on Har Sinai the commandments "I am Hashem your Lord etc." and "You should not have other gods etc." and after forty days said to the Golden Calf, 'This is our god etc.', be saved?" This is the meaning of the continuation of the previously mentioned possuk in Tehillim, "There is no salvation for him from Hashem..."
Bnei Yisroel continued; “Hashem in the Torah agreed with the Nations of the World’s argument and wrote, "A person that slaughters an animal to other gods shall be destroyed.”
However, continues Bnei Yisroel, Hashem protect us in the merit of our forefathers and instead of having our heads hung for what we did, Hashem gave us the opportunity to elevate our heads, like it says, "When you take the census of Bnei Yisroel...".
The Nations of the World argue that Bnei Yisroel should be destroyed and not be spared because we served an idol a mere forty days after we were commanded not to Bnei Yisroel agreed with their argument however they counter-argued that in the merit of our forefathers and in the merit of giving the machatzis hashekel we should be saved
Zera Shimshon asks, since Bnei Yisroel themselves agreed that they deserved to be destroyed why didn't Hashem indeed destroy Bnei Yisroel for what they did?
Before we get to his answer, a short introduction is needed. In the possuk that Bnei Yisroel mentioned to show they agreed with the Nations of the World, "A person that slaughters an animal to other gods shall be destroyed..." the word used to say "destroyed" is not the common word used in the Torah to describe someone who deserves capital punishment. The common term is "mose yamuss" or "demayim bo". The word the Torah used here is yacharum who's root is "chairem" which, in Parshas Bechukosai, means consecrating something for the Bais Hamikdash. When a person does this, it becomes forbidden for anyone to derive any pleasure from that article and therefore here it means destroyed. Since the Torah used this term to describe the death penalty for someone who serves an idol, Zera Shimshon posits that the limitations of consecrating something to Hashem also applies to the death penalty for someone who served an idol.
The Gemara (Eruchin 28a) writes; a person may consecrate for the Bais Hamikdash some of his flock, some of his cattle, or his heathen servant, but he is not able to consecrate his daughter or his Jewish servant. The difference between these two groups is that only one who owns something to such a degree that he has the power to sell it, whenever and for as long as he wants, can make something “chairem”. A person has full control of when and how to sell his cattle, his flock and his heathen servant, therefore he has the power to consecrate them. However concerning his daughter, he can only sell her as a servant until she is twelve years old, and therefore he cannot consecrate her. The ownership of a Jewish servant is governed by Halacha, therefore the owner does not have full control over his Jewish servant and hence he cannot consecrate them.
Hashem made an oath to Avraham Avinu that his descendants will inherit the Land of Canaan, and if Hashem would sell us to be slaves to another nation forever, this oath would remain unfulfilled. Therefore, since we cannot be sold forever, Hashem is not able, so to speak, to destroy us just like a person cannot consecrate his daughter or Jewish servant to the Bais Hamikdash. This is the meaning of Bnei Yisroel's counter-argument, "protect me in the merit of our forefathers" in the merit of the oath that Hashem made to Avraham we are protected from being annihilated.
Since after "all is said and done" Bnei Yisroel served the Golden Calf, and therefore they deserved to be punished, Hashem commanded them to give machtzis hashekel in order to redeem themselves from destruction (chairim) in the same way that one can redeem an article that was made chairem for the Bais Hamikdash.