The Kli Yakar on יום לשנה says that the punishment actually took place one day a year on Tish'a B'Av. Thus they were punished for "forty days" but the forty days were spread over 40 years. The punishment was that all those who should have been in the army and insisted on not listening to the spies (ages 20 to 60) were allowed to live out their "service life" and were punished by dying at age 60. Rabbi Munk says that since those who had turned 60 the previous year died on each Tish'a B'Av, it took forty years for the punishment to complete. This allowed the children (under 20) to grow up and replace them so that there was a fully trained and complete community ready to go into Canaan.
This was actually the manifestation of Hashem's Rachamim (mercy). Note that the medrash says that hashem shortened the time of the meraglim in the land so that the trip would take forty days. Rav Shimshon Rafael Hirsch says
by fitting this time of the duration of the punishment in proportion to the sin, the sin is kept in the minds of the penitents during the whole course of the duration of the punishment. THis method, by which, simply by using some external similarity of numbers, something is brought to, and kept in, the mind, is frequently used by our sages in their institutions.
Bnei Yisrael had free will and could have resisted the temptation. However, Hashem knew of the possibility of the sin and the necessity of the punishment. As a result, he shortened the trip so that the punishment could be carried out in such a way as to ensure that the lesson had been learned.