Jeff Puckett II's answer is fine, and you should follow his link to this other answer as well. But what you want is in fact built directly in to Git:
git rev-list --count master..feature
and:
git rev-list --count feature..master
which you might want to combine into very short alias or script:
echo feature is $(git rev-list --count master..feature) commits ahead of master
echo and $(git rev-list --count feature..master) commits behind master
This is, in fact, just what git status and git branch -vv do, although they check for zero-commits-ahead-or-behind first too.
Here's a short (very short, for me :-) ) explanation.
Git's log and rev-list are almost the same command
Both git log and git rev-list look at a whole range of commits. The most important difference is that git log shows you the log messages (and optionally, patches), while git rev-list shows you just the raw commit hashes, by default.
You can, however, get git rev-list to give you a count of how many commits it would have listed, instead of their raw hash IDs. And, as you will see in pretty diagrams on that other answer, the stop..start two-dot syntax (except in git diff which is very special1) tells Git "give me all the commits you can find starting from start, excluding all the commits you can find starting from stop".
If you find all the commits that are on feature, then take away all the commits that are on both branches, you're left with only the commits that are only on feature. Then you have git rev-list count them. These are the commits you're "ahead", so that's how far ahead you are.
(Having git log log them, each on one line, then getting wc to count the lines, also works, of course.)
Reversing the two names, with feature..master, has Git find commits that are on master, then take away commits on both branches. These are the ones you're "behind".
The output of git rev-list is rarely useful for humans, and is mainly intended to be used in scripts. In fact, that's just what I suggested above: a two line script (or even just one line, but that did not fit very well on my screen here).
Here's a question for thought: how do we know to use the names master and feature? How could we instruct Git to know this? But let's leave that for later.
1The issue here is that git diff desperately wants to work with at most two commits. The range notations with two and three dots, master..feature or master...feature, usually produce a long list of commits. Git's diff can't handle these anyway. It could just refuse to take these notations—which would have been quite reasonable, really—but instead, the Git authors decided they'd re-use the notations to mean something related.
This means that whatever you've learned about using master..feature with git diff, you must forget it all whenever dealing with other Git commands. Sic transit gloria Git.