Disclaimer: The following is not a solution, and may be completely useless. I just followed the assumptions and tried to see what $f$ would need to look like. So might as well post it here as a summary.
So let's say that first, we restrict our attention to $GL_n(\mathbb{R})$, the space of invertible matrices (and then we'll generalize). Then, we have two fiber bundles:

As Dietrich Burde mentioned, the most natural way to fill up this diagram is the following:
.
However, I think you're looking for a map $f : \text{Mat}_n(\mathbb{R}) \to \text{Mat}_n(\mathbb{R})$ such that $\det \circ f = \text{tr}$. So let's see what that would look like.
- First, let's make a bad assumption about this map $f$: let's assume it maps invertible matrices to invertible matrices.
In other words, it restricts to $f : GL_n(\mathbb{R}) \to GL_n(\mathbb{R})$ such that $\det \circ f = \text{tr}$. So it gives us a diagram that looks like this:

where the bottom map is the natural inclusion.
However, there are some matrices in the top right version of $GL_n(\mathbb{R})$ that have trace $0$, so whatever the map $f$ is, there is no way you can get this diagram to commute. Contradiction.
Conclusion: if such an $f$ exists, it maps some invertible matrices to non-invertible matrices. (And actually, it's easy to check that it's exactly the matrices with trace $0$ that are gonna be sent to non-invertible matrices. It actually pretty much follows from the definition of $f$).
Now going back to the general $f$. Your map $f : \text{Mat}_n(\mathbb{R}) \to \text{Mat}_n(\mathbb{R})$ would need to map matrices of trace $0$ (which is a closed, negligible set in $\text{Mat}_n(\mathbb{R})$) into the set of matrices of determinant $0$ (which is also a closed, negligible set in $\text{Mat}_n(\mathbb{R})$). And it needs to do so in a smooth way.
I've got to admit that I'm struggling to visualize what that map would look like, because both of the sets are kind of "sparse" in Mat$_n(\mathbb{R})$. It may definitely be possible (or maybe there's something forbidding it), but I can't see anything. Maybe someone will have an idea how to continue from there! (Or maybe this isn't useful at all but it was at least fun to imagine)