The reason you do not have access to that variable outside the loop is because your stash tags (getters and setters) are not parsed at the same time. EE module tags are parsed recursively from the outwards inwards, peeling them one after another like a oignon.
In your code above, your stash set tags are inside an entry loop (level 2), while your get tag is at the root (level 1). That means your get tags are still empty when processed.
Solution One:
Use process=end
on your getter to force it to be processed later than your setter
With a single value:
{exp:channel:entries channel="blogposts" disable="categories|category_fields|custom_fields|member_data|pagination" dynamic="no" limit="1"}
{exp:stash:st_entryid}{entry_id}{/exp:stash:st_entryid}
{/exp:channel:entries}
<p>Stash output entry id: {exp:stash:st_entryid process="end"}</p>
With a list
{exp:channel:entries channel="blogposts" disable="categories|category_fields|custom_fields|member_data|pagination" dynamic="no" limit="5"}
{exp:stash:append_list name="st_blogposts_ids" parse_tags="yes"}
{stash:st_entryid}{entry_id}{/stash:st_entryid}
{/exp:stash:append_list}
{/exp:channel:entries}
{exp:stash:get_list name="st_blogposts_ids" parse_conditionals="yes" process="end"}
{if count == 1}<ul>{/if}
<li>Entry ID: {st_entryid}</li>
{if count == total_results}</ul>{/if}
{/exp:stash:get_list}
Solution Two:
Put the setters and the getters on the same "oignon" layer by wrapping your setters around the channel entries tag. That's usually the method I use whenever possible. With lists, you have the added value that the performance of {exp:stash:set_list}
is better than {exp:stash:append_list}
Single value:
{exp:stash:st_testid parse_tags="yes" trim="yes"}
{exp:channel:entries channel="blogposts" disable="categories|category_fields|custom_fields|member_data|pagination" dynamic="no" limit="1"}
{entry_id}
{/exp:channel:entries}
{/exp:stash:st_testid}
<p>Stash output entry id: {exp:stash:st_testid}</p>
List:
{exp:stash:set_list name="st_test_ids" parse_tags="yes"}
{exp:channel:entries channel="blogposts" disable="categories|category_fields|custom_fields|member_data|pagination" dynamic="no" limit="5"}
{stash:st_entryid}{entry_id}{/stash:st_entryid}
{/exp:channel:entries}
{/exp:stash:set_list}
{exp:stash:get_list name="st_test_ids" parse_conditionals="yes"}
{if count == 1}<ul>{/if}
<li>Entry ID: {st_entryid}</li>
{if count == total_results}</ul>{/if}
{/exp:stash:get_list}
Hope that helps.