When do regular values form an open set? Let $f:M\to N$ be a $C^\infty$ map between manifolds.

When is the set of regular values of $N$ an open set in $N$?

There is a case which I sort of figured out:


*

*If $\operatorname{dim} M = \operatorname{dim} N$ and $M$ is compact, it is open by the following argument (fixed thanks to user7530 in the comments):


Let $y\in N$. Suppose $f^{-1}(y)\neq \emptyset$. The stack of records theorem applies: $f^{-1}(y)=\{x_1,\dots,x_n\}$ and there is an open neighborhood $U$ of $y$ such that $f^{-1}(U)=\bigcup_{i=1}^n U_i$, with $U_i$ an open neighborhood of $x_i$ such that the $U_i$ are pairwise disjoint and $f$ maps $U_i$ diffeomorphically onto $U$.
Now every point in $f^{-1}(U)$ is regular, since if $x_i'\in U_i$, then $f|_{U_i}:U_i\to U$ diffeomorphism $\Rightarrow$ $df_{x_i'}$ isomorphism (thanks to user 7530 for simplifying the argument).
Now suppose $f^{-1}(y)=\emptyset$. Then there is an open neighborhood $V$ of $y$ such that every value in $V$ has no preimages. Indeed, the set $N\setminus f(M)$ is open, since $M$ compact $\Rightarrow$ $f(M)$ compact, hence closed. Therefore $V$ is an open neighborhood of $y$ where all values are regular, and we are done.


Can we remove the compactness/dimension assumptions in some way?

 A: I think I can prove the converse of your result: if every smooth function $f: M \to N$ has regular values that form an open subset of $N$, then $M$ is compact.
I will first prove this statement for $N=\mathbb{R}^n$. Suppose, for contradiction, that $M$ is not compact. Then there is a countable locally-finite open cover $U=\{U_i\}$ of $M$ and a countable set of points $\{x_i\}$ with $x_i\in U_i$ and $x_i \not\in U_j$ for $i\neq j$.
Let $\{\phi_i\}$ be a partition of unity subordinate to $U$. Let $g(x) = \sum_i \phi_i(x)^2$. The map $g$ is a well-defined smooth function on $M$ with $g(x) \leq 1$ and $g(x_i) = 1$ for all $i$. Hence $x_i$ is a critical point of $g$, and therefore a critical point of $\phi_i$, for every $x_i$.
Let $S$ be the set of rational numbers in $[0,1]^n$. The set $S$ is countably infinite, so there exists a bijection $q: \mathbb{N} \to S.$
Finally, let 
$$f(x) = \sum_i q(i) \phi_i^2(x).$$
$f$ is a smooth function from $M$ to $[0,1]^n$. Since $x_i$ is a critical point of $\phi_i$, it is a critical point of $f$, with $f(x_i) = q(i)$. Hence no element of $S$ is a regular value of $f$. But by Sard's theorem, $f$ has a regular value $v\in[0,1]^n$. Since every neighborhood of $v$ contains a point of $S$, the set of regular values of $f$ can't be open, a contradiction.
Replacing $[0,1]^n$ with a suitable subset of a chart of $N$ gives the result for arbitrary manifold $N$.
A: The set of critical points is closed. You want that the image of this set under $f$ be closed. What about demanding that $f$ is closed? A condition that implies that $f$ is closed is to demand that $f$ is proper (i.e. preimages of compact sets are compact). 
