The best way to generate a legend from multiple columns is to use a Fold Transform to fold them into a single column, then let the encodings handle the legend for you. Modifying the example you linked to, it might look something like this: (vega editor link):
{
"data": {"url": "data/seattle-weather.csv", "format": {"type": "csv"}},
"encoding": {
"x": {"timeUnit": "yearmonthdate", "field": "date", "type": "temporal"},
"tooltip": [
{"timeUnit": "yearmonthdate", "field": "date", "type": "temporal"},
{"field": "temp_max", "type": "quantitative"},
{"field": "temp_min", "type": "quantitative"}
]
},
"layer": [
{
"transform": [
{"fold": ["temp_min", "temp_max"], "as": ["measure", "temp"]}
],
"mark": {"type": "line"},
"encoding": {
"y": {"field": "temp", "type": "quantitative"},
"color": {"field": "measure", "type": "nominal"}
}
},
{
"mark": "rule",
"selection": {
"hover": {"type": "single", "on": "mouseover", "empty": "none"}
},
"encoding": {
"color": {
"condition": {"selection": {"not": "hover"}, "value": "transparent"}
}
}
}
],
"config": {"axisY": {"minExtent": 30}}
}

Notice that we've replaced the two temp_min/temp_max layers with a single layer that transforms the data and encodes color by column name, automatically generating a legend.