How do I know whether to write the written character or printed character? I'm often finding differences in the printed form of the character than the written form, and I'm not sure which one to use. For example, in 窗, the typed character sometimes contains legs 儿, but the written character (shown in stroke diagrams) contains these 丷, or possibly 八.

And then 隔, in its typed form sometimes contains 儿 and 丅, and sometimes contains 丷 and 丅. ...It seems mostly to do with the legs.

Anyway, the question is, which version is right, and how do I know?
This question is a tautology - of course you would write the written character and not the printed character!

The written characters and printed characters nowadays are both typeface/font variations of Regular Script (楷書), originally a style written using a calligraphy brush, and which is what that stroke diagram shows:


  
  
  Regular script font imitating a calligraphy brush


The written character is now written using a pen, but still largely follows the stroke order of the original calligraphy styles, so it mainly just appears thinner than calligraphy-based fonts, and should look like the following:


  
  Japanese textbook font (教科書體, きょうかしょたい), designed specifically to imitate pen handwriting in regular script.


The printed character in your example originated in the 1100's CE Song dynasty, when movable type was first invented, and is called Song or Ming typeface (宋體/明體):


  
  
  Characteristics of this typeface are:
  
  
  Thick vertical strokes and thin horizontal strokes
  Triangles at the end of single horizontal strokes
  Geometrical regularity
  


It is the emphasis of Song/Ming typeface on geometrical balance that makes it divergent from natural handwriting, and there are deliberately introduced strokes in Song/Ming typeface characters that don't actually exist in regular handwriting.

The correct form of the character always defers to a calligraphy brush or pen based font, but there are also regional variations in what is considered 'correct'; that is, China, Hong Kong, Taiwan, Korea, and Japan all have different ways of writing Chinese characters, so you need to be clear as to what standard you're following and follow that closely.