Embedding compact (boundaryless?) $n$-manifolds in $n$-dimensional real space

Here's an alternate proof which doesn't use invariance of domain. It also gives a slightly stronger result.

Theorem:
Let $M^n$ be compact without boundary. Then there is no immersion $f:M\rightarrow \mathbb{R}^n$.

Proof: (sketch). Assume for a contradiction there is such an $f$. Since $M$ is compact, $f$ is a closed map, that is, it maps closed sets to closed sets. To see this, let $F\subseteq M$ be closed. Then it's compact because $M$ is, so $f(F)$ is compact, hence closed. (Here, we just use the fact that $f$ is continuous).

Further, $f$ is an open map. That it, it maps open sets to open sets. To see this, note that it is enough to map really small open sets to open sets because $f(\bigcup U_i) = \bigcup f(U_i)$.

It's a fact (a consequence of the implicit function theorem, if I recall) that every immersion locally looks like the natural inclusion $\mathbb{R}^k\rightarrow \mathbb{R}^n$ into the first $k$ coordinates. (This uses the boundarylessness of $M$ - if $p$ is on the boundary, this part doesn't work.)

Said more precisely, given our immersion $f$ and a point $p\in M$, there is a chart around $p$ and around $f(p)$ so that in these chart coordinates, $f$ looks like the inclusion.
Now, in our case $k = n$ and then the natural inclusion is a homeomorphism. This implies open sets in these special charts are mapped to open sets, so $f$ is open.

Putting this together, we've now seen that $f$ is an open and closed mapping. Now, what is $f(M)$? It must be compact because $M$ is, but it must also be both open and closed in $R^n$ because $M$ is open and closed in itself. This implies $f(M) = \mathbb{R}^n$ since that's the only nonempty clopen subset of $\mathbb{R}^n$, but $\mathbb{R}^n$ isn't compact, so we've reached a contradiction.


The embedded submanifolds $\emptyset \neq S\subset M$ of codimension $0$ (i.e. $dim M-dimS=0$) of a manifold $M$ are exactly the open subsets of $M$ : Lee, Introduction to smooth manifolds Proposition 5.1, page 99.
Hence they are not compact if $M$ is connected and not compact, which applies to $M=\mathbb R^n$.


I claim we can work even less hard than the arguments that have been proposed so far! Let $M$ be a closed $n$-manifold and let $f : M \to \mathbb{R}^n$ be a smooth map. Pick a coordinate $x : \mathbb{R}^n \to \mathbb{R}$. The composition $x \circ f : M \to \mathbb{R}$ attains a maximum at some point $m \in M$ (by compactness). At this point $m$, $df$ cannot be injective (because its $x$-component is zero), so $f$ is not an immersion.