“牛B” is strictly informal. Use it with close friends, peers, and online, but never in a professional setting, with elders, teachers, or people you need to show respect to.
As a Sincere Compliment: This is its most common function. It's used to praise someone's exceptional talent or achievement. For example, if a friend scores a perfect goal in a soccer game, you'd yell, “牛B!”
As a General Exclamation: Seeing an amazing stunt in a movie or a breathtaking view might elicit a simple, “哇, 太牛B了!” (Wā, tài niúbī le!) — “Wow, that's so f*cking awesome!”
In a Joking/Arrogant Way: A person might say “我太牛B了” (Wǒ tài niúbī le - I'm so awesome) after accomplishing something. Among friends, this is usually a funny boast. Said seriously, it can come across as extremely arrogant.
Written vs. Spoken: When spoken, you say the full sound “niúbī.” When typed (e.g., in text messages or on social media), “牛B” or “NB” are very common. People also use the cleaner, single-character version “牛 (niú)” or the slightly different alternative “牛叉 (niúchā).”