$S \rightarrow aSbS | bSaS | \varepsilon$
it is obvious that this produces strings with the same number of $a$'s and $b$'s.
it is less obvious that this produces all of them :
Let $s$ be such a string. If its of the form $aSb$ or $bSa$ it's in the grammar described above.
If not, the first and last letter are the same (we assume it's $a$). Let $n$ be the length of the word, and $f(x)$ the number of a's minus the number of b's in the first $x$ letters of the word.
since $f(1)=1$, and $f(n-1)=-1$, then there exists a $x
* $f(x)=0$ (at this point same number of $a$'s and $b's$)
* $f(x-1)=1$ (the previous letter is a $b$)
* $f(x+1)=-1$ (the next letter is a $b$)
Therefore the word $s$ is of the form $aubbwa$ where the length of $aub$ is $x$. Since $f(x)=0$, $u$ contains the same number of $a$'s and $b's$, and therefore $bwa$ does too. Therefore $s$ is recognized by the above grammar.