Seriously is used as slang to mean "Are you being serious?" AKA "Are you telling the truth?" As you said, it can be used when someone does something particularly stupid or interesting. But in general, it is used as a response whenever a statement is unbelievable or out of the ordinary. When you don't quite believe what someone is saying, or you want to show empathy for your friend who experienced an unusual event.
Sentences that might get a reply of "seriously" from your friend:
"I bought a phone, and then someone stole it from me the same day."
"I studied for 2 weeks, and I still failed the test."
"My father has been cheating on my other for years."
"I found a 100 dollar bill on the ground."
"I was watching TV last night, and I heard that scientists discovered the moon really IS made of cheese."
"My boss danced to Taylor Swift's music for 2 hours straight at the
company Christmas party."
All of these situations are a little hard to believe or are out of the ordinary. So it would be very common for someone to reply with "Are you serious?" or more casually "Seriously?" Sometimes it is posed as an actual question to make sure the person isn't lying or exaggerating. But more often it is just an expression to show our disbelief at how ridiculous the situation is or to express our empathy for our friend.
In addition, it can often be said by the person in that ridiculous situation. An example would be if a person buys a coffee but then someone bumps into them and makes them spill it everywhere. That person might remark to themselves "Seriously?" to question why such a ridiculous thing happened to them. Or they might even say it to the person who caused the accident, implying "Did you actually just make me spill my coffee?" Even though the person is obviously aware that it actually happened, it is a way to express our feelings of shock that the event did, in fact, occur (because it was so improbable).
Long story short, it is a phrase that expresses disbelief. And expressing disbelief can serve many purposes. It can comfort a friend by showing you also think the situation was awful. It can be used to show your joy over something unbelievably wonderful happening to your friend. It can show your disappointment when a friend acts unbelievably stupid. And it can be used to express your own frustration when something ridiculous happens to you.