Firstly, don't panic! Stay calm. If there is violent shaking you need to get under a table or bed, or something. If there is nothing, door frames are a strong part of any house, so stay under them, or otherwise slip into your toilet. Stay away from windows, cabinets, etc., as things will fly and fall over.
Avoid running outside necessarily, as it can be very dangerous. Things fall off buildings easily, so if you are going outside, make sure to be aware of what is falling from above you.
After a big shake, be aware that there is likely to be another one. It could be an after shock, but there could actually be something bigger coming. Either way, there is a high chance of another shake. Expect it, wait for it.
If it is a large shake and things look pretty bad, you should move towards your nearest evacuation site. Most of these sites tend to be schools, but there are other public places also. Evacuation maps are readily available from your local government offices, so make sure you get a copy and know where your evacuation site is in advance.
If you are in Tokyo or a big city after a large shake, there will be chaos with the transportation systems afterwards, and you may be quite severely impacted. From experience, if you can, stay where you are. It tends to be a lot better to spend the night in your office on the floor (hopefully with others for support) rather than trying to get home amongst the mayhem of traffic jams and millions of other workers.
For this reason, it is a good idea to keep supplies in your office; enough dried food and water for 2-3 days.
I have attached a couple of documents that are good examples of what to do.