$\DeclareMathOperator{tr}{tr}$
$\DeclareMathOperator{grad}{grad}$
$\DeclareMathOperator{sym}{sym}$
New answer:
WLOG, We can assume that $B$ is positive definite. Since for every orthogonal matrix $U$, we have
$$ \tr(UAU^T(B+\lambda I)) = \tr(UAU^TB) + \lambda \tr(A)$$
So if we use $B+\lambda I$ (for sufficiently large $\lambda$) instead of $B$, the optimal solution set were not changed.
Old answer:
Let us, first, find critical points of the problem. One can see the problem as an unconstrained optimization problem on the manifold of orthogonal matrices. So first order optimality conditions are $\grad f(U) = 0$, where $\grad$ stand for the Riemannian gradient of $f(U) := \tr(UAU^TB)$ on the orthogonal matrices.
We have $\grad f(U) = 2AU^TB-2U^T \sym(UAU^TB)$, where $\sym$ is the symmetric part of a matrix.
Now, necessary optimality conditions reduce to
$B$ commutes with $UAU^T$.
So when $B$ is a diagonal matrix (not necessarily semi-positive definite) with distinct elements, $UAU^T$ is diagonal and columns of $U^T$ are eigenvectors of $A$.