The answer to your question is not really know yet and there is whole scientific field dedicated to it: developmental biology. I'll try to explain the basics however:
Like you described the identity of a cell (its cell type; which organ it belongs to) is dependent on which of (actually) ~30000 human genes are activated (or read) in that cell. There are two fundamentally different ways this is done in cells:
- Transcription factors:
These are proteins that bind to specific (regulatory) DNA sequences and thereby lead to the activation (or repression) of certain - usually nearby - genes. Since they are proteins, their own genes have to be activated in the first place, so we already at a 'chicken or the egg' type of problem. In principle proteins can 'survive' outside the nucleus of the cell for some time, but they are usually degraded within a few hours, so I guess the remaining factors from the mother cell are not very important (I have no idea if anyone ever checked this).
Additionally many transcription factors are not constitutively active, but are waiting for some outside (from the perspective of the cell) signal. These could be certain developmental signals, growth factors, hormones or environmental factors like nutrient availability (the list goes on ...). During embryo (& therefore organ) development not only the presence of these signals (morphogens), but also their intensity are a very important factor in determining the 'fate' of a given cell.
The term epigenetics describes genetic regulation that does not happen on the sequence level. Instead the accessibility of the DNA is regulated through modifications of histones (which are part of the chromosomes) and the DNA itself. Thereby the cell can almost permanently shut down certain genes. Since epigenetics are also inherited during cell division, this regulation affects whole cell lineages and is therefore very important in regulating which cells are active in certain organs or tissues.