That the characters of representations have modular properties is the content of Kac-Peterson [1]. As for an "explanation" of this, there are several but I'm afraid all of the ones I know involve in one way or the other physics. In his seminal paper [2] Zhu proves modularity for Vertex Operator Algebras with certain conditions. This explains in particular the statement in your question (at the affine Lie algebra level). However, Zhu came to his result by studying the correlation functions of these algebras on the torus, so I'll try to expand minimally on the topic although you'll find a beautiful exposition in Frenkel-Ben-Zvi's book [3].

The idea is as follows, to each vertex operator algebra $V$ (the one associated to an affine Kac-Moody Lie algebra in the case of your question) and an algebraic curve $X$ you can associate a vector space of coinvariants of $V$, $C(X,V)$. As the curve $X$ moves in the moduli space of curves of a given genus, the vector spaces $C(X,V)$ arrange to form a vector bundle on this space (it is generally a quasi-coherent sheaf, but under certain finiteness condition which holds in your case it is a bundle). This can be generalized by looking at pointed curves and modules of $V$ over the marked points of $X$. In the particular case that you look at the moduli space of elliptic curves, the sections of this bundle are the characters of the representations (where $q = e^{2 \pi i \tau}$ is the modular parameter of the corresponding curve). This is a "reason" for those characters to have modular properties, however, the fact that we expected already these spaces to form a bundle on $M_g$ comes from CFTs considerations.

[1] Kac, Victor G.(1-MIT); Peterson, Dale H.(1-MI) Infinite-dimensional Lie algebras, theta functions and modular forms. Adv. in Math. 53 (1984), no. 2, 125–264.

[2] Zhu, Yongchang(HK-HKST) Modular invariance of characters of vertex operator algebras. J. Amer. Math. Soc. 9 (1996), no. 1, 237–302.

[3] Frenkel, Edward; Ben-Zvi, David Vertex algebras and algebraic curves. (English summary) Mathematical Surveys and Monographs, 88. American Mathematical Society, Providence, RI, 2001. xii+348 pp. ISBN: 0-8218-2894-0