First, this paper of Kirby, Melvin and Teichner (which I haven't completely digested) discusses the case of maps from smooth, closed, connected, oriented 4-manifolds $M$ to $S^3$. Theorem 1 is a refinement of the following: $[M,S^3]$ is an abelian group with a surjective homomorphism to $H_1(M)$ which is either an isomorphism or a two-to-one epimorphism, depending on whether $M$ contains at least one closed oriented surface of odd self-intersection ($M$ is "odd") or if no such surface exists ($M$ is "even").
What follows is an elaboration on the case of 3-manifolds, already treated by Piotr Haljasz.
The beginning of Section 3 of this paper of DeTurck, Gluck, Komendarczyk, Melvin, Shonkwiler, and Vela-Vick discusses the homotopy classification of maps from oriented closed smooth 3-manifolds to $S^2$ (originally due to Pontryagin).
To summarize, homotopy classes of maps from $M$ to $S^2$ (elements of $[M,S^2]$) are classified by two invariants. First, the primary invariant $\lambda(f)\in H_1(M,\mathbb{Z})$ is the homology class of the inverse image $f^{-1}(\ast)$ for any regular value $\ast$ of $f$ (and $\lambda(f)$ can take any value in $H_1(M,\mathbb{Z})$). The secondary invariant $\nu(f_0,f_1)\in\mathbb{Z}_{2d(\lambda)}$ compares two maps $f_0,f_1$ with the same primary invariant $\lambda$, where $d(\lambda)$ is the divisibility of $\lambda$ as an element of $H_1(M,\mathbb{Z})/\text{torsion}$, i.e. $d(\lambda)=0$ if $\lambda$ is of finite order and otherwise equal to the largest positive integer $d$ such that $d\lambda=\kappa$ for some $\kappa\in H_1(M,\mathbb{Z})$. Again, $\nu(f_0,f_1)$ can take on all values in $\mathbb{Z}_{2d(\lambda)}$.
This classification gives us an infinite number of homotopically nontrivial maps to $S^2$: in particular, the maps with $\lambda=0$ are in bijection with $\mathbb{Z}$.
In the language of the above invariants, Piotr Hajlasz's answer constructs a map $f=h\circ g$ with $\lambda(f)=0$ and $\nu(\text{const}, f)=1$ (where "const" is a constant map $M\rightarrow S^2$) and proves that it is not nullhomotopic.
As an aside: the Pontryagin-Thom theorem states that homotopy classes of maps from $M$ to $S^2$ are in bijection with bordism classes of framed links in $M$ and this turns out to be a beautiful and powerful way of visualizing such maps. The two invariants above can be viewed as bordism invariants of framed links: for example, the maps with $\lambda=0$ correspond to bordism classes of the unknot with framing $m$ for all $m\in\mathbb{Z}$. See the discussion in the reference of DeTurck et al and also check out the figures there which illustrate the classification for $M=T^3$.
One ought to be able to apply Pontryagin-Thom to better understand elements of $[M^n,S^{n-1}]$ for any $n$ (they still correspond to bordism classes of framed links in $M$) and I suspect it has been done but I don't know of a reference which does this in general.
Note that when $n>3$ the framing of the unknot now only takes values in $\mathbb{Z}/2\mathbb{Z}$, essentially because $\pi_1(SO(n-1))\cong\mathbb{Z}/2\mathbb{Z}$, which I discussed at the end of this old MO answer. In the $n=4$ result of Kirby, Melvin and Teichner, this appears in the fact that the kernel of the homomorphism from $[M,S^3]$ to $H_1(M)$ has at most order 2; see the discussion in the subsection "Twisted Classes" on page 165 and also Claim 1.1 in their paper.