How do you write an emulator?

I have read some tutorials online and articles online about it but I am still confused on how to actually write one. The tutorials/articles talk about how an emulator works but how do you actually go about writing one? For example if someone wrote a Mac emulator then how does your program know how to handle a dmg file? Read byte after byte but what exactly do you do with these bytes?

I was thinking of writing one for fun in Java but if any other language is required then that is fine (i.e. Assembly).