Note that this is currently maintained in the Examples project so the code here may not be the latest.
In this example, we just use checkboxes to determine whether textboxes are displayed.
One of the fundamental ideas of having a form change based on selections within the form is that the form is reconfiguring itself based on $form_state. So here, the generation of the form is driven by $form_state['values']. If the checkbox for last name is checked, then we generate a textfield for last name.