Doing something painfully means doing it in a way that hurts a whole lot, like painfully stubbing your bare toe on the sidewalk when you're showing off on your new skateboard.
If something occurs in a way that causes pain, it happens painfully. This can range from painfully pulling a muscle when you're playing tennis to painfully coughing when you've got a sore throat. You can also use this adverb figuratively, for something that seems exaggerated or extreme: "The novel was painfully overwrought, full of overly dramatic prose" or "My brother is painfully shy."