Tor exposes a text-based control protocol so other software can talk to it. Tor Browser uses it to show circuit details and to send the NEWNYM signal for New Identity. The monitoring tool nyx uses it to draw bandwidth graphs. OnionShare uses it to create temporary onion services on the fly.
A standalone tor daemon conventionally uses port 9051 for control and 9050 for SOCKS; Tor Browser's bundled tor uses 9151 and 9150 so the two don't collide. You turn it on in torrc:
ControlPort 9051
CookieAuthentication 1
Authentication matters. Anything that can talk to the control port can reconfigure Tor, read circuit information and, in some setups, learn things that undermine your anonymity. Cookie authentication (a secret file only readable by permitted users) or a hashed password via HashedControlPassword should always be on. Never expose the port beyond localhost.
For scripting, the Python library Stem is the usual choice. Whonix goes further and puts a filter in front of the control port so the workstation can only send a whitelisted set of commands to the gateway.