What is the difference between square brackets and curly brackets in a command invocation?

Pardon the naive beginner question.

In a command like: \textcolor[HTML]{4C7045}{dark green text}, or \documentclass[12pt]{article}, or \begin{figure}[htb], why are some arguments surrounded by [] and some by {}?

The above examples are all for LaTeX, but this doesn't seem to be LaTeX-specific.

• This really is LaTeX-specific: the [] optional convention really doesn't apply to plain, ConTeXt or other formats. – Joseph Wright Jun 2 '14 at 8:41
• Is \newcommand from LaTeX, then? How is it defined? – user253751 Jun 2 '14 at 8:42
• @immbis Yes, \newcommand is LaTeX-specific: it's wrapper around the \def primitive and depending on the exact set up does various things (optional arguments, for example, require more than one TeX macro for implementation). – Joseph Wright Jun 2 '14 at 8:46
• There is also <n>; used, for example, in beamer. – gerrit Jun 2 '14 at 14:41

In TeX (and LaTeX and ConTeXt) braces ({}) are used as grouping characters.
In LaTeX, brackets ([]) are conventionally used for optional arguments, whereas braces are used for mandatory arguments.
• Nota bene: xargs (and probably others) make it easy to screw the conventions in LaTeX. – Raphael Jun 2 '14 at 19:30
• @Raphael Of course: the answer doe say 'conventionally'. Notice that xparse, written by the team, does allow the creation of non-standard input syntaxes but the idea is that it's primarily for creating standard ones: \foo*[<opt>]{<mandatory-1>}. – Joseph Wright Jun 4 '14 at 9:10
In general everything between [...] is optional and everything between {...} is mandatory. However, it depends to the author of the macros what will be optional and what will be mandatory. There are a lot of other possibilities.