They're all about a concept of come and go.
In a speaker's position, if the subject finally go away, then we add 가다. And if the subject finally come to the speaker, we use 오다.
Yes! Examples!
들어가다 /들어오다 ; go in (enter) / come in
If I enter a room, I say 들어가다. But someone inside the room says 들어오다.
A(outside the room) : 안녕하세요? 들어가도 될까요?
B(inside the room) : 네, 들어오세요.
돌아가다/돌아오다; go back / come back
돌아가다 means go back, 돌아오다 means come back.
A : Where's Hanna? 한나 어디있어?
B : She went back to Korea. 한나는 한국으로 돌아갔어.
In B's position, 한나 went from B, so we use 돌아가다.
돌아와 돌아와 내겐 아직 너뿐인걸
Come back. Come back. I still want only you. blabla~
The speaker wants someone to come back to him/her.
나가다/나오다 ; go out / come out
He went out of the room. 걔는 방에서 나갔어.
I came out of the room. 나는 방에서 나왔어.
다녀가다/다녀오다 ?
They're also understood in this point of view. 다녀가다/오다 means come by or visit somewhere.
If someone came by my office and went away, I say 누가 다녀갔어요.
If I visit another's office and came home, I say 사무실에 다녀왔어요.
To sum up, 가다/오다 is a matter of go/come. So Always think about whether you are in or out of the place and a final movement is going away from or coming to the speaker.