What I know about this is a little dated and is mostly useful to an Assembler programmer, but I'll have a go at it.
Free space on a single volume can be obtained by the LSPACE macro. In 2005 I wrote a line mode TSO command that in its heart is a shell for the LSPACE macro.
Extent Free Free Free Free Frag Mount
Unit Type Volser Status Cyls:trk Cyls Extents DSCBs VIR Index Status
0A80 3390 Z6RES1 S 159:11 516 48 1119 300 244 PRIVATE
This program will require quite a lot of attention to support extended attribute type volumes.
As Mr. Sample indicates, this knowledge is not very useful these days since most allocation is done using SMS.
Free space within a data set is more complex. Unused space at the end of a data set can be calculated by obtaining the Format 1 DSCB and any Format 3 DSCBs for the data set, using data in them to determine the allocated space for the data set, and then using the contents of DS1LSTAR to determine the used space. Subtract used space from allocated space to get unused space. This is from a TSO command originally written in the early 1980s, and rewritten in 1991.
0A80 3390 Z6RES1 FB 80 6160 PO 2745 2737
04/03/01 08/10/10 ------ YES
As with the LSPACE command, this program will require attention for extended attribute volumes.
VSAM is a more complex problem since unused space is distributed through the data set. The LISTCAT command in IDCAMS will show the high allocated and high used RBA for a VSAM data set, but this information may not be very reliable, and it gives no indication of available space within the data set.