How to correctly display page numbering in a custom footer (with and without total number of pages)?

Typst internally already handles this case for the default footer (or header) by checking if the given numbering pattern has two or more counting symbols. You can apply the same logic manually in your footer:

#set page(footer: context {
  // Handle the case when page.numbering is not set by
  // falling back to the default "1" numbering pattern.
  let page-numbering = page.numbering
  if page-numbering == none { page-numbering = "1" }

  let both = type(page-numbering) == function or {
    page-numbering.clusters().filter(c => c in (
      // Counting symbols: https://typst.app/docs/reference/model/numbering
      "1", "a", "A", "i", "I", "α", "Α", "*",
      "א", "一", "壹", "あ", "い", "ア", "イ", "ㄱ",
      "가", "\u{0661}", "\u{06F1}", "\u{0967}",
      "\u{09E7}", "\u{0995}", "①", "⓵"
    )).len() >= 2
  }

  numbering(
    page-numbering,
    ..counter(page).get(),
    ..if both { counter(page).at(<numbering-main-end>) }
  )
})

Note that I have already included the counting symbols that are not released yet (as of v0.12.0) but are part of current development builds. They are not shown in the documentation yet, but can be retrieved from the source code.