The Torah itself establishes the first day of the seventh month as a day of sounding, commanding in Vayikra 23:24 a "sacred occasion commemorated with loud blasts" and in Bamidbar 29:1 that "it shall be a day when the horn is sounded" — the scriptural foundation for the entire practice.
The Gemara in Rosh Hashanah 16a supplies two distinct reasons: the shofar accompanies the recitation of Malkhiyyot, Zikhronot, and Shofarot so that Israel will crown God as King and so that their remembrance will rise before Him for good, and Rabbi Abbahu adds that a ram's horn is used specifically so that God will recall the Binding of Yitzchak and reckon it in Israel's favor.
Building on this, the Rambam (Mishneh Torah, Repentance 3:4) articulates a moral-spiritual dimension: although the blowing is a divine decree, it carries an implicit message — "Awake, you sleepers, from your sleep; arise, you slumberers, from your slumber; examine your deeds and return in repentance and remember your Creator" — and the Guide for the Perplexed, Part 3:43 confirms this by describing Rosh Hashanah as a day on which people are stirred from their religious forgetfulness, which is precisely why the shofar is blown.
The Tur in Tur, Orach Chayim 581 adds a historical rationale rooted in Chazal's tradition: when Moshe ascended Sinai on the first of Elul to receive the second tablets, the shofar was sounded throughout the camp so Israel would not stray after idolatry, and the Sages thereafter ordained annual shofar-blowing to warn Israel to repent — and also, the Tur notes, to confound the Adversary.