At the moment the contents of the pseudo file /proc/cpuinfo is read by various programs to determine the Pi model. The revision field gives the needed information.
A revision < 4 is an early model B.
A revision < 16 is a model A or B.
A revision of 17 is a compute module.
A revision of 16 or > 17 has a 40 pin expansion header.
I give that list as from that you can work out which GPIO are available. You can break it down further to identify the precise model and board revision.
For full information you will have to treat the revision number as a 26 bit binary number as follows
2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
W W S M M M B B B B P P P P T T T T T T T T R R R R
WW warranty void if either bit is set
S 0=old (bits 0-22 are revision number) 1=new (following fields apply)
MMM 0=256 1=512 2=1024
BBBB 0=SONY 1=EGOMAN 2=EMBEST 3=UNKNOWN 4=EMBEST
PPPP 0=2835, 1=2836, 2=2837
TTTT 0=A 1=B 2=A+ 3=B+ 4=Pi2B 5=Alpha 6=Compute Module 7=unknown 8=Pi3B 9=Zero
RRRR PCB board revision
I am not aware of any standard for identifying if a HAT is fitted or what it might be. That may be an oversight in the HAT standard.