The brain is split into two main parts, instinct and memory. The instincts are pre-coded so to speak when your born, but the memory section is blank, much for the same reason you can't remember everything you have ever seen heard felt etc in that there is only a limited amount of info that could be processed.
The brain basically links instincts to memories. As they're encoded new situations lead to new interconnects between hardwired instincts as too what could happen at any given moment when you see a situation. Your brain searches it's instinct register to try and figure out what could happen as a means of survival then if it finds it searches for a memory to link it to.
If you've never experienced the situation, then there is no memory, if it has a precoded instinct in that situation the brain will trigger the danger or safe instinct. If you come across a situation that you've never seen before, then the brain unable to link it to any instinct records the memory, and hard codes an instinct based on it. When faced with the situation again.. your instinct will then kick in cuz you've done it before much like riding a bike in that sense.
As for the issue of just how they're passed, When your born your brain is only so big.. it can only hold so much "info" so the memories are discarded, but the instincts are passed on as you grow and your brain does as well. the memory section is developed and the cycle starts again.. constantly looking for new info in which to hard code the next generations instincts.