How can Hashem set up witnesses before Yisrael have sinned?
Rashi: He set them up - not to testify that they had sinned, but as witnesses that He had warned them. 1
Rashbam: This was an advance warning that the Heaven and earth will come and punish them in the event that they sin. 2
See Sifsei Chachamim.
Rashbam: As the Torah will write in Eikev, 11:17. "ve'Atzar es ha'Shamayim ... ve'ha'Aretz Lo Sitein es Yevulah". And as it states in Parshas Shoftim, 17:7 "Yad ha'Eidim Tih'yeh bo ba'Rishonah ... ".
Why did Hashem pick specifically heaven and earth as witnesses?
Oznayim la'Torah: Because, for some reason, they were the only creations that the people did not worship (so they were not destined to be destroyed).
How can Heaven and earth testify whether Yisrael fulfilled the Torah, seeing as the testimony affects themselves, as the Navi writes in Yirmiyah 33:25 - "Im Lo B'risi
Refer to 4:26:1:1.
Moshav Zekenim: Indeed, they are not believed to testify that Yisrael fulfilled Torah, but they would be believed to testify that they transgressed Torah, for this harms themselves. 1
Testimony that harms the witness himself is accepted. It is unlike a relative, whose testimony is Pasul whether it helps or harms his relative (Shach CM 37:1 derives this from Rema 37:17).